Day 2 - ipynb

This commit is contained in:
Jonathan Frederic 2015-05-29 15:31:44 -07:00
parent bc2a2ecde2
commit 4c008765fe
5 changed files with 70 additions and 423 deletions

View File

@ -11,16 +11,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configuring the IPython Notebook"
"## Configuring the Jupyter Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook web server can also be configured using IPython profiles and configuration files. The Notebook web server configuration options are set in a file named `ipython_notebook_config.py` in your IPython *profile directory*. The profile directory is a subfolder of your IPython directory, which itself is usually `.ipython` in your home directory.\n",
"The notebook web server can also be configured using Jupyter profiles and configuration files. The Notebook web server configuration options are set in a file named `jupyter_notebook_config.py` in your Jupyter directory, which itself is usually `.jupyter` in your home directory."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default version of `jupyter_notebook_config.py` lists all of the options available along with documentation for each. Changes made to that file will affect all notebook servers run under that profile. Command line options always override those set in configuration files.\n",
"\n",
"You can display the location of your default profile directory by running the command:"
"You can create a new config:"
]
},
{
@ -31,61 +38,14 @@
},
"outputs": [],
"source": [
"!ipython profile locate default"
"!jupyter notebook --generate-config"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The default version of `ipython_notebook_config.py` lists all of the options available along with documentation for each. Changes made to that file will affect all notebook servers run under that profile. Command line options always override those set in configuration files.\n",
"\n",
"You can create a new profile:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!ipython profile create my_profile"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And then view its location:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!ipython profile locate my_profile"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To start the notebook server using a given profile, run the following:\n",
"\n",
" ipython notebook --profile=my_profile"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"More details about IPython configuration files and profiles can be found [here](http://ipython.org/ipython-doc/dev/config/intro.html)."
"More details about Jupyter configuration files and profiles can be found [here](http://ipython.org/ipython-doc/dev/config/intro.html)."
]
},
{
@ -99,7 +59,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The IPython Notebook allows arbitrary code execution on the computer running it. Thus, the notebook web server should never be run on the open internet without first securing it. By default, the notebook server only listens on local network interface (`127.0.0.1`) There are two steps required to secure the notebook server:\n",
"The Jupyter Notebook allows arbitrary code execution on the computer running it. Thus, the notebook web server should never be run on the open internet without first securing it. By default, the notebook server only listens on local network interface (`127.0.0.1`) There are two steps required to secure the notebook server:\n",
"\n",
"1. Setting a password\n",
"2. Encrypt network traffic using SSL"
@ -136,7 +96,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can then add this to your `ipython_notebook_config.py`:\n",
"You can then add this to your `jupyter_notebook_config.py`:\n",
"\n",
"```python\n",
"# Password to use for web authentication\n",
@ -171,14 +131,14 @@
"\t\n",
"When you enable SSL support, you will need to access the notebook server over ``https://``, rather than plain ``http://``. The startup message from the notebook server prints the correct URL, but it is easy to overlook and think the server is for some reason non-responsive.\n",
"\n",
"Once you have generated the key and certificate, you can configure the notebook server to use them, by adding the following to `ipython_notebook_config.py`:\n",
"Once you have generated the key and certificate, you can configure the notebook server to use them, by adding the following to `jupyter_notebook_config.py`:\n",
"\n",
"```python\n",
"# The full path to an SSL/TLS certificate file.\n",
"c.NotebookApp.certfile = u'/Users/bgranger/.ipython/profile_my_profile/mycert.crt'\n",
"c.NotebookApp.certfile = u'/Users/bgranger/.jupyter/mycert.crt'\n",
"\n",
"# The full path to a private key file for usage with SSL/TLS.\n",
"c.NotebookApp.keyfile = u'/Users/bgranger/.ipython/profile_my_profile/mycert.key'\n",
"c.NotebookApp.keyfile = u'/Users/bgranger/.jupyter/mycert.key'\n",
"```"
]
},
@ -201,9 +161,9 @@
"\n",
"This can be accomplished by passing a command line options.\n",
"\n",
" ipython notebook --ip=* --no-browser\n",
" jupyter notebook --ip=* --no-browser\n",
"\n",
"You can also add the following to your`ipython_notebook_config.py` file:\n",
"You can also add the following to your `jupyter_notebook_config.py` file:\n",
"\n",
"```python\n",
"c.NotebookApp.ip = '*'\n",
@ -223,7 +183,7 @@
"metadata": {},
"source": [
"The notebook dashboard typically lives at the URL `http://localhost:8888/tree`. If you prefer that it lives, together with the \n",
"rest of the notebook web application, under a base URL prefix, such as `http://localhost:8888/ipython/tree`, you can do so by adding the following lines to your `ipython_notebook_config.py` file.\n",
"rest of the notebook web application, under a base URL prefix, such as `http://localhost:8888/ipython/tree`, you can do so by adding the following lines to your `jupyter_notebook_config.py` file.\n",
"\n",
"```python\n",
"c.NotebookApp.base_url = '/ipython/'\n",
@ -287,7 +247,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.4.0"
}
},
"nbformat": 4,

View File

@ -20,12 +20,12 @@
"source": [
"The traditional IPython (`ipython`) consists of a single process that combines a terminal based UI with the process that runs the users code.\n",
"\n",
"While this traditional application still exists, the modern IPython consists of two processes:\n",
"While this traditional application still exists, the modern Jupyter consists of two processes:\n",
"\n",
"* Kernel: this is the process that runs the users code.\n",
"* Frontend: this is the process that provides the user interface where the user types code and sees results.\n",
"\n",
"IPython currently has 3 frontends:\n",
"Jupyter currently has 3 frontends:\n",
"\n",
"* Terminal Console (`ipython console`)\n",
"* Qt Console (`ipython qtconsole`)\n",
@ -124,7 +124,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.4.0"
}
},
"nbformat": 4,

View File

@ -1,317 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NbConvert"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Command line usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`NbConvert` is both a library and command line tool that allows you to convert notebooks to other formats. It ships with many common formats: `html`, `latex`, `markdown`, `python`, `rst`, and `slides`\n",
"NbConvert relys on the Jinja templating engine, so implementing a new format or tweeking an existing one is easy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can invoke nbconvert by running\n",
"\n",
"```bash\n",
"$ ipython nbconvert <options and arguments>\n",
"```\n",
"\n",
"Call `ipython nbconvert` with the `--help` flag or without any aruments to display the basic help. For detailed configuration help, use the `--help-all` flag."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Basic export"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a test, the `Index.ipynb` notebook in the directory will be convert. \n",
"\n",
"If you're converting a notebook with code in it, make sure to run the code cells that you're interested in before attempting to convert the notebook. Unless explicitly requested, nbconvert **does not execute the code cells** of the notebooks that it converts."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"ipython nbconvert 'Index.ipynb'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Html is the (configurable) default value. The verbose form of the same command as above is "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"ipython nbconvert --to=html 'Index.ipynb'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also convert to latex, which will extract the embeded images. If the embeded images are SVGs, inkscape is used to convert them to pdf:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"ipython nbconvert --to=latex 'Index.ipynb'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the latex conversion creates latex, not a PDF. To create a PDF you need the required third party packages to compile the latex.\n",
"\n",
"A `--post` flag is provided for convinience which allows you to have nbconvert automatically compile a PDF for you from your output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"ipython nbconvert --to=latex 'Index.ipynb' --post=pdf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom templates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Look at the first 20 lines of the `python` exporter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pyfile = !ipython nbconvert --to python 'Index.ipynb' --stdout\n",
"for l in pyfile[20:40]:\n",
" print l"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the code, you can see that non-code cells are also exported. If you want to change this behavior, you can use a custom template. The custom template inherits from the Python template and overwrites the markdown blocks so that they are empty."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%writefile simplepython.tpl\n",
"{% extends 'python.tpl'%}\n",
"\n",
"{% block markdowncell -%}\n",
"{% endblock markdowncell %}\n",
"\n",
"## we also want to get rig of header cell\n",
"{% block headingcell -%}\n",
"{% endblock headingcell %}\n",
"\n",
"## and let's change the appearance of input prompt\n",
"{% block in_prompt %}\n",
"# This was input cell with prompt number : {{ cell.prompt_number if cell.prompt_number else ' ' }}\n",
"{%- endblock in_prompt %}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pyfile = !ipython nbconvert --to python 'Index.ipynb' --stdout --template=simplepython.tpl\n",
"\n",
"for l in pyfile[4:40]:\n",
" print l\n",
"print '...'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For details about the template syntax, refer to [Jinja's manual](http://jinja2.readthedocs.org/en/latest/intro.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Template that use cells metadata"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook file format supports attaching arbitrary JSON metadata to each cell. Here, as an exercise, you will use the metadata to tags cells."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First you need to choose another notebook you want to convert to html, and tag some of the cells with metadata. You can refere to the file `soln/celldiff.js` as an example or follow the Javascript tutorial to figure out how do change cell metadata. Assuming you have a notebook with some of the cells tagged as `Easy`|`Medium`|`Hard`|`<None>`, the notebook can be converted specially using a custom template. Design your template in the cells provided below.\n",
"\n",
"The following, unorganized lines of code, may be of help:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"{% extends 'html_full.tpl'%}\n",
"{% block any_cell %}\n",
"{{ super() }}\n",
"<div style=\"background-color:red\">\n",
"<div style='background-color:orange'>\n",
"```\n",
"\n",
"If your key name under `cell.metadata.example.difficulty`, the following code would get the value of it:\n",
"\n",
"`cell['metadata'].get('example',{}).get('difficulty','')`\n",
"\n",
"Tip: Use `%%writefile` to edit the template in the notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"# ipython nbconvert --to html <your chosen notebook.ipynb> --template=<your template file>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%loadpy soln/coloreddiff.tpl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# ipython nbconvert --to html '04 - Custom Display Logic.ipynb' --template=soln/coloreddiff.tpl"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get rid of all command line flags."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IPython nbconvert can be configured using the default profile or a profile specified via the `--profile` flag. Additionally, if a `config.py` file exist in current working directory, nbconvert will use that as config."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -4,19 +4,19 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing IPython Notebooks as Modules"
"# Importing Jupyter Notebooks as Modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is a common problem that people want to import code from IPython Notebooks.\n",
"It is a common problem that people want to import code from Jupyter Notebooks.\n",
"This is made difficult by the fact that Notebooks are not plain Python files,\n",
"and thus cannot be imported by the regular Python machinery.\n",
"\n",
"Fortunately, Python provides some fairly sophisticated [hooks](http://www.python.org/dev/peps/pep-0302/) into the import machinery,\n",
"so we can actually make IPython notebooks importable without much difficulty,\n",
"so we can actually make Jupyter notebooks importable without much difficulty,\n",
"and only using public APIs."
]
},
@ -117,7 +117,7 @@
"outputs": [],
"source": [
"class NotebookLoader(object):\n",
" \"\"\"Module Loader for IPython Notebooks\"\"\"\n",
" \"\"\"Module Loader for Jupyter Notebooks\"\"\"\n",
" def __init__(self, path=None):\n",
" self.shell = InteractiveShell.instance()\n",
" self.path = path\n",
@ -126,7 +126,7 @@
" \"\"\"import a notebook as a module\"\"\"\n",
" path = find_notebook(fullname, self.path)\n",
" \n",
" print (\"importing IPython notebook from %s\" % path)\n",
" print (\"importing Jupyter notebook from %s\" % path)\n",
" \n",
" # load the notebook object\n",
" with io.open(path, 'r', encoding='utf-8') as f:\n",
@ -193,7 +193,7 @@
"outputs": [],
"source": [
"class NotebookFinder(object):\n",
" \"\"\"Module finder that locates IPython Notebooks\"\"\"\n",
" \"\"\"Module finder that locates Jupyter Notebooks\"\"\"\n",
" def __init__(self):\n",
" self.loaders = {}\n",
" \n",
@ -515,7 +515,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.4.0"
}
},
"nbformat": 4,

View File

@ -99,48 +99,41 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To inject Javascript we provide an entry point: `custom.js` that allow teh user to execute and load other resources into the notebook.\n",
"To inject Javascript we provide an entry point: `custom.js` that allow the user to execute and load other resources into the notebook.\n",
"Javascript code in `custom.js` will be executed when the notebook app start and can then be used to customise almost anything in the UI and in the behavior of the notebook.\n",
"\n",
"`custom.js` can be found in IPython profile dir, and so you can have different UI modification on a per profile basis, as well as share your modfication with others."
"`custom.js` can be found in the Jupyter dir. You can share your custom.js with others."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Because we like you...."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have been provided with an already existing profile folder with this tutorial...\n",
"start the notebook from the root of the tutorial directory with :\n",
"\n",
"```bash\n",
"$ ipython notebook --ProfileDir.location=./profile_euroscipy\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### but back to theory"
"##### Back to theory"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'/home/jon/.jupyter'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"profile_dir = ! ipython locate\n",
"profile_dir = profile_dir[0]\n",
"import os.path\n",
"profile_dir = '~/.jupyter'\n",
"profile_dir = os.path.expanduser(profile_dir)\n",
"profile_dir"
]
},
@ -153,28 +146,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os.path\n",
"custom_js_path = os.path.join(profile_dir,'profile_default','static','custom','custom.js')"
"custom_js_path = os.path.join(profile_dir,'custom','custom.js')"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You don't have a custom.js file\n"
]
}
],
"source": [
"# my custom js\n",
"with open(custom_js_path) as f:\n",
" for l in f: \n",
" print l,"
"if os.path.isfile(custom_js_path):\n",
" with open(custom_js_path) as f:\n",
" for l in f: \n",
" print(l)\n",
"else:\n",
" print(\"You don't have a custom.js file\") "
]
},
{
@ -215,7 +219,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Have a look at [default custom.js](https://github.com/ipython/ipython/blob/1.x/IPython/html/static/custom/custom.js), to see it's content and some more explanation."
"Have a look at [default custom.js](https://github.com/ipython/ipython/blob/3.x/IPython/html/static/custom/custom.js), to see it's content and some more explanation."
]
},
{
@ -604,7 +608,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.4.0"
}
},
"nbformat": 4,