From fbb0829bbc872507ddd4a5b9f3b4b136c3f8d24c Mon Sep 17 00:00:00 2001 From: Carol Willing Date: Tue, 22 Mar 2016 18:28:43 -0700 Subject: [PATCH] Remove rst versions of nb --- docs/source/conf.py | 3 +- .../Connecting with the Qt Console.rst | 67 --- .../rstversions/Custom Keyboard Shortcuts.rst | 71 ---- .../Examples and Tutorials Index.rst | 34 -- .../rstversions/Importing Notebooks.rst | 286 ------------- .../JavaScript Notebook Extensions.rst | 389 ------------------ .../Notebook/rstversions/Notebook Basics.rst | 266 ------------ .../Notebook/rstversions/Running Code.rst | 154 ------- .../rstversions/Typesetting Equations.rst | 290 ------------- .../What is the Jupyter Notebook.rst | 136 ------ .../Working With Markdown Cells.rst | 313 -------------- .../examples/Notebook/rstversions/index.rst | 37 -- docs/source/index.rst | 11 +- 13 files changed, 12 insertions(+), 2045 deletions(-) delete mode 100644 docs/source/examples/Notebook/rstversions/Connecting with the Qt Console.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Custom Keyboard Shortcuts.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Examples and Tutorials Index.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Importing Notebooks.rst delete mode 100644 docs/source/examples/Notebook/rstversions/JavaScript Notebook Extensions.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Notebook Basics.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Running Code.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Typesetting Equations.rst delete mode 100644 docs/source/examples/Notebook/rstversions/What is the Jupyter Notebook.rst delete mode 100644 docs/source/examples/Notebook/rstversions/Working With Markdown Cells.rst delete mode 100644 docs/source/examples/Notebook/rstversions/index.rst diff --git a/docs/source/conf.py b/docs/source/conf.py index 43783f5df..17e917bc1 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -70,6 +70,7 @@ extensions = [ 'sphinx.ext.mathjax', 'IPython.sphinxext.ipython_console_highlighting', 'sphinxcontrib.spelling', + 'nbsphinx', ] # Add any paths that contain templates here, relative to this directory. @@ -78,7 +79,7 @@ templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] -source_suffix = '.rst' +source_suffix = ['.rst', '.ipynb'] # The encoding of source files. #source_encoding = 'utf-8-sig' diff --git a/docs/source/examples/Notebook/rstversions/Connecting with the Qt Console.rst b/docs/source/examples/Notebook/rstversions/Connecting with the Qt Console.rst deleted file mode 100644 index 906b2548a..000000000 --- a/docs/source/examples/Notebook/rstversions/Connecting with the Qt Console.rst +++ /dev/null @@ -1,67 +0,0 @@ - -`View the original notebook on nbviewer `__ - -Connecting to an existing IPython kernel using the Qt Console -============================================================= - -The Frontend/Kernel Model -------------------------- - -The traditional IPython (``ipython``) consists of a single process that -combines a terminal based UI with the process that runs the users code. - -While this traditional application still exists, the modern Jupyter -consists of two processes: - -- Kernel: this is the process that runs the users code. -- Frontend: this is the process that provides the user interface where - the user types code and sees results. - -Jupyter currently has 3 frontends: - -- Terminal Console (``ipython console``) -- Qt Console (``ipython qtconsole``) -- Notebook (``ipython notebook``) - -The Kernel and Frontend communicate over a ZeroMQ/JSON based messaging -protocol, which allows multiple Frontends (even of different types) to -communicate with a single Kernel. This opens the door for all sorts of -interesting things, such as connecting a Console or Qt Console to a -Notebook's Kernel. For example, you may want to connect a Qt console to -your Notebook's Kernel and use it as a help browser, calling ``??`` on -objects in the Qt console (whose pager is more flexible than the one in -the notebook). - -This Notebook describes how you would connect another Frontend to a -Kernel that is associated with a Notebook. - -Manual connection ------------------ - -To connect another Frontend to a Kernel manually, you first need to find -out the connection information for the Kernel using the -``%connect_info`` magic: - -.. code:: python - - %connect_info - -You can see that this magic displays everything you need to connect to -this Notebook's Kernel. - -Automatic connection using a new Qt Console -------------------------------------------- - -You can also start a new Qt Console connected to your current Kernel by -using the ``%qtconsole`` magic. This will detect the necessary -connection information and start the Qt Console for you automatically. - -.. code:: python - - a = 10 - -.. code:: python - - %qtconsole - -`View the original notebook on nbviewer `__ diff --git a/docs/source/examples/Notebook/rstversions/Custom Keyboard Shortcuts.rst b/docs/source/examples/Notebook/rstversions/Custom Keyboard Shortcuts.rst deleted file mode 100644 index 6cf95e22d..000000000 --- a/docs/source/examples/Notebook/rstversions/Custom Keyboard Shortcuts.rst +++ /dev/null @@ -1,71 +0,0 @@ - -`View the original notebook on nbviewer `__ - -Keyboard Shortcut Customization -=============================== - -Starting with IPython 2.0 keyboard shortcuts in command and edit mode -are fully customizable. These customizations are made using the Jupyter -JavaScript API. Here is an example that makes the ``r`` key available -for running a cell: - -.. code:: python - - %%javascript - - Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', { - help : 'run cell', - help_index : 'zz', - handler : function (event) { - IPython.notebook.execute_cell(); - return false; - }} - ); - -"By default the keypress ``r``, while in command mode, changes the type -of the selected cell to ``raw``. This shortcut is overridden by the code -in the previous cell, and thus the action no longer be available via the -keypress ``r``." - -There are a couple of points to mention about this API: - -- The ``help_index`` field is used to sort the shortcuts in the - Keyboard Shortcuts help dialog. It defaults to ``zz``. -- When a handler returns ``false`` it indicates that the event should - stop propagating and the default action should not be performed. For - further details about the ``event`` object or event handling, see the - jQuery docs. -- If you don't need a ``help`` or ``help_index`` field, you can simply - pass a function as the second argument to ``add_shortcut``. - -.. code:: python - - %%javascript - - Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) { - IPython.notebook.execute_cell(); - return false; - }); - -Likewise, to remove a shortcut, use ``remove_shortcut``: - -.. code:: python - - %%javascript - - Jupyter.keyboard_manager.command_shortcuts.remove_shortcut('r'); - -If you want your keyboard shortcuts to be active for all of your -notebooks, put the above API calls into your ``custom.js`` file. - -Of course we provide name for majority of existing action so that you do -not have to re-write everything, here is for example how to bind ``r`` -back to it's initial behavior: - -.. code:: python - - %%javascript - - Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', 'jupyter-notebook:change-cell-to-raw'); - -`View the original notebook on nbviewer `__ diff --git a/docs/source/examples/Notebook/rstversions/Examples and Tutorials Index.rst b/docs/source/examples/Notebook/rstversions/Examples and Tutorials Index.rst deleted file mode 100644 index 5c1dd4caa..000000000 --- a/docs/source/examples/Notebook/rstversions/Examples and Tutorials Index.rst +++ /dev/null @@ -1,34 +0,0 @@ - -`View the original notebook on nbviewer `__ - - - -Examples and Tutorials -====================== - -This portion of the documentation was generated from notebook files. You -can download the original interactive notebook files using the links at -the tops and bottoms of the pages. - -Tutorials ---------- - -- `What is the Jupyter - Notebook `__ -- `Notebook Basics `__ -- `Running Code `__ -- `Working With Markdown - Cells `__ -- `Custom Keyboard Shortcuts `__ -- `JavaScript Notebook - Extensions `__ - -Examples --------- - -- `Importing Notebooks `__ -- `Connecting with the Qt - Console `__ -- `Typesetting Equations `__ - -`View the original notebook on nbviewer `__ diff --git a/docs/source/examples/Notebook/rstversions/Importing Notebooks.rst b/docs/source/examples/Notebook/rstversions/Importing Notebooks.rst deleted file mode 100644 index 390ae9953..000000000 --- a/docs/source/examples/Notebook/rstversions/Importing Notebooks.rst +++ /dev/null @@ -1,286 +0,0 @@ - -`View the original notebook on nbviewer `__ - -Importing Jupyter Notebooks as Modules -====================================== - -It is a common problem that people want to import code from Jupyter -Notebooks. This is made difficult by the fact that Notebooks are not -plain Python files, and thus cannot be imported by the regular Python -machinery. - -Fortunately, Python provides some fairly sophisticated -`hooks `__ into the import -machinery, so we can actually make Jupyter notebooks importable without -much difficulty, and only using public APIs. - -.. code:: python - - import io, os, sys, types - -.. code:: python - - from IPython import get_ipython - from IPython.nbformat import current - from IPython.core.interactiveshell import InteractiveShell - -Import hooks typically take the form of two objects: - -1. a Module **Loader**, which takes a module name (e.g. - ``'IPython.display'``), and returns a Module -2. a Module **Finder**, which figures out whether a module might exist, - and tells Python what **Loader** to use - -.. code:: python - - def find_notebook(fullname, path=None): - """find a notebook, given its fully qualified name and an optional path - - This turns "foo.bar" into "foo/bar.ipynb" - and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar - does not exist. - """ - name = fullname.rsplit('.', 1)[-1] - if not path: - path = [''] - for d in path: - nb_path = os.path.join(d, name + ".ipynb") - if os.path.isfile(nb_path): - return nb_path - # let import Notebook_Name find "Notebook Name.ipynb" - nb_path = nb_path.replace("_", " ") - if os.path.isfile(nb_path): - return nb_path - - -Notebook Loader ---------------- - -Here we have our Notebook Loader. It's actually quite simple - once we -figure out the filename of the module, all it does is: - -1. load the notebook document into memory -2. create an empty Module -3. execute every cell in the Module namespace - -Since IPython cells can have extended syntax, the IPython transform is -applied to turn each of these cells into their pure-Python counterparts -before executing them. If all of your notebook cells are pure-Python, -this step is unnecessary. - -.. code:: python - - class NotebookLoader(object): - """Module Loader for Jupyter Notebooks""" - def __init__(self, path=None): - self.shell = InteractiveShell.instance() - self.path = path - - def load_module(self, fullname): - """import a notebook as a module""" - path = find_notebook(fullname, self.path) - - print ("importing Jupyter notebook from %s" % path) - - # load the notebook object - with io.open(path, 'r', encoding='utf-8') as f: - nb = current.read(f, 'json') - - - # create the module and add it to sys.modules - # if name in sys.modules: - # return sys.modules[name] - mod = types.ModuleType(fullname) - mod.__file__ = path - mod.__loader__ = self - mod.__dict__['get_ipython'] = get_ipython - sys.modules[fullname] = mod - - # extra work to ensure that magics that would affect the user_ns - # actually affect the notebook module's ns - save_user_ns = self.shell.user_ns - self.shell.user_ns = mod.__dict__ - - try: - for cell in nb.worksheets[0].cells: - if cell.cell_type == 'code' and cell.language == 'python': - # transform the input to executable Python - code = self.shell.input_transformer_manager.transform_cell(cell.input) - # run the code in themodule - exec(code, mod.__dict__) - finally: - self.shell.user_ns = save_user_ns - return mod - - -The Module Finder ------------------ - -The finder is a simple object that tells you whether a name can be -imported, and returns the appropriate loader. All this one does is -check, when you do: - -.. code:: python - - import mynotebook - -it checks whether ``mynotebook.ipynb`` exists. If a notebook is found, -then it returns a NotebookLoader. - -Any extra logic is just for resolving paths within packages. - -.. code:: python - - class NotebookFinder(object): - """Module finder that locates Jupyter Notebooks""" - def __init__(self): - self.loaders = {} - - def find_module(self, fullname, path=None): - nb_path = find_notebook(fullname, path) - if not nb_path: - return - - key = path - if path: - # lists aren't hashable - key = os.path.sep.join(path) - - if key not in self.loaders: - self.loaders[key] = NotebookLoader(path) - return self.loaders[key] - - -Register the hook ------------------ - -Now we register the ``NotebookFinder`` with ``sys.meta_path`` - -.. code:: python - - sys.meta_path.append(NotebookFinder()) - -After this point, my notebooks should be importable. - -Let's look at what we have in the CWD: - -.. code:: python - - ls nbpackage - -So I should be able to ``import nbimp.mynotebook``. - -Aside: displaying notebooks -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Here is some simple code to display the contents of a notebook with -syntax highlighting, etc. - -.. code:: python - - from pygments import highlight - from pygments.lexers import PythonLexer - from pygments.formatters import HtmlFormatter - - from IPython.display import display, HTML - - formatter = HtmlFormatter() - lexer = PythonLexer() - - # publish the CSS for pygments highlighting - display(HTML(""" - - """ % formatter.get_style_defs() - )) - -.. code:: python - - def show_notebook(fname): - """display a short summary of the cells of a notebook""" - with io.open(fname, 'r', encoding='utf-8') as f: - nb = current.read(f, 'json') - html = [] - for cell in nb.worksheets[0].cells: - html.append("

%s cell

" % cell.cell_type) - if cell.cell_type == 'code': - html.append(highlight(cell.input, lexer, formatter)) - else: - html.append("
%s
" % cell.source) - display(HTML('\n'.join(html))) - - show_notebook(os.path.join("nbpackage", "mynotebook.ipynb")) - -So my notebook has a heading cell and some code cells, one of which -contains some IPython syntax. - -Let's see what happens when we import it - -.. code:: python - - from nbpackage import mynotebook - -Hooray, it imported! Does it work? - -.. code:: python - - mynotebook.foo() - -Hooray again! - -Even the function that contains IPython syntax works: - -.. code:: python - - mynotebook.has_ip_syntax() - -Notebooks in packages ---------------------- - -We also have a notebook inside the ``nb`` package, so let's make sure -that works as well. - -.. code:: python - - ls nbpackage/nbs - -Note that the ``__init__.py`` is necessary for ``nb`` to be considered a -package, just like usual. - -.. code:: python - - show_notebook(os.path.join("nbpackage", "nbs", "other.ipynb")) - -.. code:: python - - from nbpackage.nbs import other - other.bar(5) - -So now we have importable notebooks, from both the local directory and -inside packages. - -I can even put a notebook inside IPython, to further demonstrate that -this is working properly: - -.. code:: python - - import shutil - from IPython.utils.path import get_ipython_package_dir - - utils = os.path.join(get_ipython_package_dir(), 'utils') - shutil.copy(os.path.join("nbpackage", "mynotebook.ipynb"), - os.path.join(utils, "inside_ipython.ipynb") - ) - -and import the notebook from ``IPython.utils`` - -.. code:: python - - from IPython.utils import inside_ipython - inside_ipython.whatsmyname() - -This approach can even import functions and classes that are defined in -a notebook using the ``%%cython`` magic. - -`View the original notebook on nbviewer `__ diff --git a/docs/source/examples/Notebook/rstversions/JavaScript Notebook Extensions.rst b/docs/source/examples/Notebook/rstversions/JavaScript Notebook Extensions.rst deleted file mode 100644 index 5a465cbc2..000000000 --- a/docs/source/examples/Notebook/rstversions/JavaScript Notebook Extensions.rst +++ /dev/null @@ -1,389 +0,0 @@ - -`View the original notebook on nbviewer `__ - -Embracing web standards -======================= - -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 -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 - development of small extensions that customize the behavior of -the web interface. - -Tampering with the Notebook application ---------------------------------------- - -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. - -- In Chrome and Safari, Developer tools are in the menu - ``View > Developer > Javascript Console`` -- In Firefox you might need to install - `Firebug `__ - -Those will be your best friends to debug and try different approaches -for your extensions. - -Injecting JS -~~~~~~~~~~~~ - -Using magics -^^^^^^^^^^^^ - -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 an 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 you 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 -starts and can then be used to customize almost anything in the UI and -in 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 - - from jupyter_core.paths import jupyter_config_dir - jupyter_dir = jupyter_config_dir() - jupyter_dir - -and custom js is in - -.. code:: python - - import os.path - custom_js_path = os.path.join(jupyter_dir, 'custom', 'custom.js') - -.. code:: python - - # my custom js - if os.path.isfile(custom_js_path): - with open(custom_js_path) as f: - print(f.read()) - 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 for 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 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. - -Create a dropdown element in the toolbar (DOM -``Jupyter.toolbar.element``), you will need - -- ``Jupyter.notebook.set_autosave_interval(miliseconds)`` -- know that 1 min = 60 sec, and 1 sec = 1000 ms - -.. code:: javascript - - - var label = jQuery('