`View the original notebook on nbviewer `__ Embracing web standards ======================= <<<<<<< HEAD One of the main reason that allowed us to develop the current notebook web application was to embrace the web technology. By being a pure web application using HTML, Javascript and CSS, the ======= One of the main reasons why we developed the current notebook web application was to embrace the web technology. By being a pure web application using HTML,Javascript,and CSS, the >>>>>>> de44f5b... Update JavaScript Notebook Extensions.rst Notebook can get all the web technology improvement for free. Thus, as browser support for different media extend, the notebook web app should be able to be compatible without modification. This is also true with performance of the User Interface as the speed of JavaScript VM increases. 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. Even if this task is not always easy, we strive to keep our code as accessible and reusable as possible. This should allow us with minimum effort to develop small extensions that customize the behavior of the web interface. Tampering with Notebook app --------------------------- <<<<<<< HEAD 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 run the frontend. - In Chrome and safari Developer tools are in the menu [Put menu name in english here] - In firefox you might need to install ======= The first tool that is availlable 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 allow you to inspect/modify the DOM, and interact with the javascript code that runs the frontend. - In Chrome and Safari, developer tools are in the menu [Put menu name in English here] - In Firefox you might need to install >>>>>>> de44f5b... Update JavaScript Notebook Extensions.rst `Firebug `__ - Others ? Those will be your best friends to debug and try different approaches for your extensions. Injecting JS ~~~~~~~~~~~~ Using magics ^^^^^^^^^^^^ <<<<<<< HEAD Above tools can be tedious to edit long javascript files. Hopefully 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 an ======= The above tools can be tedious for editing 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 an >>>>>>> de44f5b... Update JavaScript Notebook Extensions.rst refining a script. 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. 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. This can still be useful in some cases, like the ``%autosave`` magic that allows to control the time between each save. But this can be replaced by a JavaScript dropdown menu to select the save interval. .. code:: python ## you can inspect the autosave code to see what it does. %autosave?? custom.js ^^^^^^^^^ To inject Javascript we provide an entry point: ``custom.js`` that allows the user to execute and load other resources into the notebook. Javascript code in ``custom.js`` will be executed when the notebook app <<<<<<< HEAD start and can then be used to customize almost anything in the UI and in ======= starts and can then be used to customise almost anything in the UI and in >>>>>>> de44f5b... Update JavaScript Notebook Extensions.rst the behavior of the notebook. ``custom.js`` can be found in the Jupyter dir. You can share your custom.js with others. Back to theory '''''''''''''' .. code:: python import os.path profile_dir = '~/.jupyter' profile_dir = os.path.expanduser(profile_dir) profile_dir and custom js is in .. code:: python import os.path custom_js_path = os.path.join(profile_dir,'custom','custom.js') .. code:: python # my custom js if os.path.isfile(custom_js_path): with open(custom_js_path) as f: for l in f: print(l) else: print("You don't have a custom.js file") 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. **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**. Exercise : ---------- - Create a ``custom.js`` in the right location with the following content: .. code:: javascript alert("hello world from custom.js") - Restart your server and open any notebook. - Be greeted by custom.js Have a look at `default custom.js `__, to see it's content and some more explanation. For the quick ones : ^^^^^^^^^^^^^^^^^^^^ 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 everytime, and that I <<<<<<< HEAD don't like to embed into my workflow and documents. (reader don't care what my autosave time is), let's build an extension that allow to do it. Create a dropdown element in the toolbar (DOM ======= don't like to embed into my workwlow and documents (readers don't care what my autosave time is). Let's build an extension that allows us to do it. Create a dropdown elemment in the toolbar (DOM >>>>>>> de44f5b... Update JavaScript Notebook Extensions.rst ``Jupyter.toolbar.element``), you will need - ``IPython.notebook.set_autosave_interval(miliseconds)`` - know that 1min = 60 sec, and 1 sec = 1000 ms .. code:: javascript var label = jQuery('