PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/index.html

https://gitlab.com/mlnkv/kajero
HTML | 303 lines | 291 code | 12 blank | 0 comment | 0 complexity | b316ce813c79f82e964fc9c09a844d60 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <meta http-equiv="content-type" content="text/html; charset=UTF8">
  6. <link rel="stylesheet" href="dist/main.css">
  7. </head>
  8. <body>
  9. <script type="text/markdown" id="kajero-md">
  10. ---
  11. title: Sample Kajero notebook
  12. author: Joel Auterson
  13. created: Mon Apr 18 2016 21:48:01 GMT+0100 (BST)
  14. datasources:
  15. joelotter: https://api.github.com/users/joelotter/repos
  16. popular: https://api.github.com/search/repositories?q=created:>2016-01-01&sort=stars&order=desc
  17. extra: https://api.github.com/search/repositories?q=created:>2016-01-01&sort=stars&order=desc&per_page=100
  18. original:
  19. title: Blank Kajero notebook
  20. url: http://www.joelotter.com/kajero/blank
  21. show_footer: true
  22. ---
  23. This is an example of a notebook written in **Kajero**. Hopefully it'll give you a bit of insight into what Kajero is, and what you might be able to use it for!
  24. Kajero is designed to make it really easy for anyone to create good-looking, responsive, data-rich documents. They're totally viewable in a web browser - no backend necessary beyond what's needed to host the page - and can contain interactive code samples, written in JavaScript.
  25. They've also got some nice graphing and data visualisation capabilities - we'll look at that in a bit.
  26. (If you were wondering, _'kajero'_ is the Esperanto word for _'notebook'_.)
  27. Let's have a look at some features.
  28. ## It's just Markdown
  29. Go ahead and take a look at the [page source](view-source:http://www.joelotter.com/kajero). You'll notice that the notebook is really just a Markdown document with some bookending HTML tags - it's the Kajero script that does all the work of rendering. You've got all the usability of Markdown to play with.
  30. - You
  31. - can
  32. - make
  33. - lists!
  34. \[ Escapes work \]
  35. There is also inline code, like `console.log('thing')`.
  36. <i>HTML is allowed, as per the Markdown spec.</i>
  37. <pre style="width: 50%;"><code>print "You can use inline HTML to get styling, which is neat.</code></pre>
  38. ```python
  39. print "You can have code, with syntax highlighting."
  40. print "This is Python - it can't be run in the browser."
  41. ```
  42. ```javascript; runnable
  43. return "Javascript can be, though. Click play!";
  44. ```
  45. Because it's just Markdown, and all the work is done by a script in the browser, it's really easy for users to create new notebooks from scratch. But you might not want to create from scratch, so...
  46. ## Every notebook is editable
  47. You might have noticed the little pencil icon in the top-right. Give it a poke! You'll find yourself in the editor interface. Every single Kajero notebook is fully editable right in the browser. Once you've made your changes, you can export it as a new HTML or Markdown document.
  48. The notebooks also contain a link to their parent page. It's in the footer of this page if you want to have a look! If users don't want this footer, it can be turned off in the editor.
  49. This is all very well, but the notebooks are supposed to be _interactive_.
  50. ## Running code
  51. Authors need to be able to put code samples in their documents. If these samples are in JavaScript, they can be run by the users. Here's a very simple example, which squares and sums the numbers up to 10.
  52. ```javascript; runnable
  53. var result = 0;
  54. for (var i = 1; i <= 10; i++) {
  55. result += i * i;
  56. }
  57. return result;
  58. ```
  59. Code samples are written (and run) as functions, and the function's returned value is displayed to the user in the box below the code. What if we want to share information between code samples, though?
  60. In Kajero, the keyword **this** refers to the global context, which is passed around between samples. We can assign something onto the context, and then access it in another sample.
  61. ```javascript; runnable
  62. this.number = 100;
  63. return this.number;
  64. ```
  65. We can now sum the squares of numbers up to the number we defined in the previous code block.
  66. ```javascript; runnable
  67. var result = 0;
  68. for (var i = 10; i <= this.number; i++) {
  69. result += i * i;
  70. }
  71. return result;
  72. ```
  73. ```javascript; runnable
  74. this.number *= 2;
  75. return this.number;
  76. ```
  77. Try playing around with running these code samples in different orders, to see how the results change.
  78. ## Working with data
  79. If you had a look in the editor, you'll have noticed that users can define _data sources_ - these are URLs of public JSON data. This data is automatically fetched, and put into the **data** object, which is made available in code samples.
  80. The **joelotter** data source is my GitHub repository information. Let's get the names of my repositories.
  81. ```javascript; runnable
  82. return data.joelotter.map(function(repo) {
  83. return repo.name;
  84. });
  85. ```
  86. You'll notice that Kajero can visualise whatever data you throw at it - it's not just strings and numbers! Here's the whole of my repository data to demonstrate.
  87. ```javascript; runnable
  88. return data.joelotter;
  89. ```
  90. This isn't necessarily the most attractive or user-friendly way to look at data, though.
  91. ## Graphs
  92. Kajero gives users access to [d3](https://d3js.org/), the web's favourite graphing library.
  93. ```javascript; runnable
  94. // Remove any old SVGs for re-running
  95. d3.select(graphElement).selectAll('*').remove();
  96. var sampleSVG = d3.select(graphElement)
  97. .append("svg")
  98. .attr("width", 100)
  99. .attr("height", 100);
  100. sampleSVG.append("circle")
  101. .style("stroke", "gray")
  102. .style("fill", "white")
  103. .attr("r", 40)
  104. .attr("cx", 50)
  105. .attr("cy", 50)
  106. .on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
  107. .on("mouseout", function(){d3.select(this).style("fill", "white");})
  108. .on("mousedown", animateFirstStep);
  109. function animateFirstStep(){
  110. d3.select(this)
  111. .transition()
  112. .delay(0)
  113. .duration(1000)
  114. .attr("r", 10)
  115. .each("end", animateSecondStep);
  116. };
  117. function animateSecondStep(){
  118. d3.select(this)
  119. .transition()
  120. .duration(1000)
  121. .attr("r", 40);
  122. };
  123. return "Try clicking the circle!";
  124. ```
  125. Users get access to **d3**, which is the library itself, and **graphElement**, which is a reference to the element where the graph is drawn.
  126. d3 is incredibly powerful, but may be too complex for many users. To help out with this, Kajero also includes [NVD3](http://nvd3.org/), which provides some nice pre-built graphs for d3. The code below generates a random scatter graph - try it!
  127. ```javascript; runnable
  128. d3.select(graphElement).selectAll('*').remove();
  129. d3.select(graphElement).append('svg').attr("width", "100%");
  130. nv.addGraph(function() {
  131. var chart = nv.models.scatter()
  132. .margin({top: 20, right: 20, bottom: 20, left: 20})
  133. .pointSize(function(d) { return d.z })
  134. .useVoronoi(false);
  135. d3.select(graphElement).selectAll("svg")
  136. .datum(randomData())
  137. .transition().duration(500)
  138. .call(chart);
  139. nv.utils.windowResize(chart.update);
  140. return chart;
  141. });
  142. function randomData() {
  143. var data = [];
  144. for (i = 0; i < 2; i++) {
  145. data.push({
  146. key: 'Group ' + i,
  147. values: []
  148. });
  149. for (j = 0; j < 100; j++) {
  150. data[i].values.push({x: Math.random(), y: Math.random(), z: Math.random()});
  151. }
  152. }
  153. return data;
  154. }
  155. return "Try clicking the rerun button!";
  156. ```
  157. This is useful too, but what about those users with little-to-no coding experience?
  158. ## Jutsu
  159. Kajero includes Jutsu, a very simple graphing library built with support for [Smolder](https://www.github.com/JoelOtter/smolder).
  160. Smolder is a 'type system' (not really, but I'm not sure what to call it) for JavaScript, which will attempt to automatically restructure arbitrary data to fit a provided schema for a function. The actual reshaping is done by a library called, predictably, [Reshaper](https://www.github.com/JoelOtter/reshaper).
  161. From a user's perspective, the details don't really matter. Let's use Jutsu (available in Kajero code samples as **graphs**) to create a pie chart, based on the most popular GitHub repositories of 2016.
  162. ```javascript; runnable
  163. // Here's what the 'popular' data looks like before it's reshaped.
  164. return data.popular;
  165. ```
  166. ```javascript; runnable
  167. // The graph functions return the reshaped data, so we can see
  168. // what's going on.
  169. return graphs.pieChart(data.popular);
  170. ```
  171. It's worked! Smolder knows that a pie chart needs labels and numerical values, so it's reshaped the data to get these.
  172. However, it's picked the first number it could find for the value, which in this case looks to be the repo IDs. This isn't really useful for a pie chart! We'd rather look at something like the number of stargazers. We can pass in a 'hint', to tell Jutsu which value we care about.
  173. ```javascript; runnable
  174. return graphs.pieChart(data.popular, 'stargazers_count');
  175. ```
  176. We can give multiple hints. Let's say we want to use the name of the repository.
  177. ```javascript; runnable
  178. return graphs.pieChart(data.popular, ['name', 'stargazers_count']);
  179. ```
  180. Good, that's a bit more readable.
  181. It's kind of hard to compare the stargazers counts in a pie chart - they're all relatively similar. Let's try a bar chart instead.
  182. ```javascript; runnable
  183. return graphs.barChart(data.popular, 'Repo', 'Stargazers', ['name', 'stargazers_count']);
  184. ```
  185. This is a bit more useful. We can put labels on the axes too, to make sure the graph is easy to understand.
  186. The idea is that it should be possible to use Kajero to investigate and write about trends in data. Let's conduct a toy investigation of our own - is there any relation between a repository's star count and the number of open issues it has?
  187. Let's try a line graph.
  188. ```javascript; runnable
  189. return graphs.lineChart(
  190. data.popular.items, 'Open Issues', 'Stargazers',
  191. ['open_issues', 'stargazers_count', 'name']
  192. );
  193. ```
  194. The extra hint, _name_, is used to provide labels for the data points. All the graphs are interactive - try mousing over them.
  195. It's pretty easy to see which repository has the most open issues (for me it's chakra-core; it might have changed by the time you read this!) and which has the most stargazers. However, it's hard to see a trend here.
  196. A much better graph for investigating correlation is a scatter plot.
  197. ```javascript; runnable
  198. return graphs.scatterPlot(
  199. data.popular.items, 'Open Issues', 'Stargazers',
  200. ['open_issues', 'stargazers_count', 'name']
  201. );
  202. ```
  203. There might be a trend there, but it's hard to see. Maybe we need more data.
  204. The GitHub API lets us request up to 100 results per page, with a default of 30. While the **popular** data source just uses the default, I've also included **extra**, which has 100. Let's try our scatter plot with 100 data points!
  205. ```javascript; runnable
  206. return graphs.scatterPlot(
  207. data.extra.items, 'Open Issues', 'Stargazers',
  208. ['open_issues', 'stargazers_count', 'name']
  209. );
  210. ```
  211. This is a little better. We can see there might be a slight positive correlation, though there are a lot of outliers.
  212. ## What's next?
  213. Hopefully this notebook has given you a decent explanation of what Kajero is for. Here are the next things needing done:
  214. - Exporting the notebook
  215. - Making Reshaper smarter
  216. - More graphs
  217. - Exporting to Gist (if there's time!)
  218. Why not try making your own notebook? This one is forked from a [blank notebook](http://www.joelotter.com/kajero/blank) - have a play with the editor!
  219. </script>
  220. <div id="kajero"></div>
  221. <script type="text/javascript" src="dist/bundle.js"></script>
  222. </body>
  223. </html>