{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Embracing web standards" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the main reasons why we developed the current notebook web application \n", "was to embrace the web technology. \n", "\n", "By being a pure web application using HTML, JavaScript, and CSS, the Notebook can get \n", "all the web technology improvement for free. Thus, as browser support for different \n", "media extend, the notebook web app should be able to be compatible without modification. \n", "\n", "This is also true with performance of the User Interface as the speed of JavaScript VM increases. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The other advantage of using only web technology is that the code of the interface is fully accessible to the end user and is modifiable live.\n", "Even if this task is not always easy, we strive to keep our code as accessible and reusable as possible.\n", "This should allow us - with minimum effort - development of small extensions that customize the behavior of the web interface. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tampering with the Notebook application" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first tool that is available to you and that you should be aware of are browser \"developers tool\". The exact naming can change across browser and might require the installation of extensions. But basically they can allow you to inspect/modify the DOM, and interact with the JavaScript code that runs the frontend.\n", "\n", " - In Chrome and Safari, Developer tools are in the menu `View > Developer > JavaScript Console` \n", " - In Firefox you might need to install [Firebug](http://getfirebug.com/)\n", " \n", "Those will be your best friends to debug and try different approaches for your extensions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Injecting JS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using magics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above tools can be tedious for editing edit long JavaScript files. Therefore we provide the `%%javascript` magic. This allows you to quickly inject JavaScript into the notebook. Still the JavaScript injected this way will not survive reloading. Hence, it is a good tool for testing and refining a script.\n", "\n", "You might see here and there people modifying css and injecting js into the notebook by reading file(s) and publishing them into the notebook.\n", "Not only does this often break the flow of the notebook and make the re-execution of the notebook broken, but it also means that you need to execute those cells in the entire notebook every time you need to update the code.\n", "\n", "This can still be useful in some cases, like the `%autosave` magic that allows you to control the time between each save. But this can be replaced by a JavaScript dropdown menu to select the save interval." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "## you can inspect the autosave code to see what it does.\n", "%autosave??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### custom.js" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To inject JavaScript we provide an entry point: `custom.js` that allows the user to execute and load other resources into the notebook.\n", "JavaScript code in `custom.js` will be executed when the notebook app starts and can then be used to customize almost anything in the UI and in the behavior of the notebook.\n", "\n", "`custom.js` can be found in the `~/.jupyter/custom/custom.js`. You can share your custom.js with others." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Back to theory" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from jupyter_core.paths import jupyter_config_dir\n", "jupyter_dir = jupyter_config_dir()\n", "jupyter_dir" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and custom js is in " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os.path\n", "custom_js_path = os.path.join(jupyter_dir, 'custom', 'custom.js')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# my custom js\n", "if os.path.isfile(custom_js_path):\n", " with open(custom_js_path) as f:\n", " print(f.read())\n", "else:\n", " print(\"You don't have a custom.js file\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that `custom.js` is meant to be modified by user. When writing a script, you can define it in a separate file and add a line of configuration into `custom.js` that will fetch and execute the file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Warning** : even if modification of `custom.js` takes effect immediately after browser refresh (except if browser cache is aggressive), *creating* a file in `static/` directory needs a **server restart**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Create a `custom.js` in the right location with the following content:\n", "```javascript\n", "alert(\"hello world from custom.js\")\n", "```\n", "\n", " - Restart your server and open any notebook.\n", " - Be greeted by custom.js" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Have a look at [default custom.js](https://github.com/jupyter/notebook/blob/4.0.x/notebook/static/custom/custom.js), to see it's content and for more explanation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For the quick ones : " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We've seen above that you can change the autosave rate by using a magic. This is typically something I don't want to type every time, and that I don't like to embed into my workflow and documents. (readers don't care what my autosave time is). Let's build an extension that allows us to do it. " ] }, { "cell_type": "markdown", "metadata": { "foo": true }, "source": [ "Create a dropdown element in the toolbar (DOM `Jupyter.toolbar.element`), you will need \n", "\n", "- `Jupyter.notebook.set_autosave_interval(milliseconds)`\n", "- know that 1 min = 60 sec, and 1 sec = 1000 ms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```javascript\n", "\n", "var label = jQuery('