Last active
March 21, 2017 20:34
-
-
Save ruxi/5a095eaf0a62033cacf61175e7a415d5 to your computer and use it in GitHub Desktop.
How to import jupyter notebook V4 (ipynb) as a python module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:32:19.192465Z", | |
"start_time": "2017-03-21T15:32:19.183649-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Last updated: 21 March 2017 03:32:19 PM'" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import datetime; f\"Last updated: {str(datetime.datetime.now().strftime('%d %B %Y %I:%M:%S %p'))}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Notes: import ipynb as py modules\n", | |
"\n", | |
"author: github.com/ruxi\n", | |
"\n", | |
"Created: 21 March 2017 03:16:56 PM" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Version information" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:33:55.337454Z", | |
"start_time": "2017-03-21T15:33:55.316160-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The version_information extension is already loaded. To reload it, use:\n", | |
" %reload_ext version_information\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/json": { | |
"Software versions": [ | |
{ | |
"module": "Python", | |
"version": "3.6.0 64bit [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]" | |
}, | |
{ | |
"module": "IPython", | |
"version": "5.3.0" | |
}, | |
{ | |
"module": "OS", | |
"version": "Linux 3.16.0 33 generic x86_64 with debian jessie sid" | |
}, | |
{ | |
"module": "notebook", | |
"version": "4.4.1" | |
} | |
] | |
}, | |
"text/html": [ | |
"<table><tr><th>Software</th><th>Version</th></tr><tr><td>Python</td><td>3.6.0 64bit [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]</td></tr><tr><td>IPython</td><td>5.3.0</td></tr><tr><td>OS</td><td>Linux 3.16.0 33 generic x86_64 with debian jessie sid</td></tr><tr><td>notebook</td><td>4.4.1</td></tr><tr><td colspan='2'>Tue Mar 21 15:33:55 2017 CDT</td></tr></table>" | |
], | |
"text/latex": [ | |
"\\begin{tabular}{|l|l|}\\hline\n", | |
"{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", | |
"Python & 3.6.0 64bit [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] \\\\ \\hline\n", | |
"IPython & 5.3.0 \\\\ \\hline\n", | |
"OS & Linux 3.16.0 33 generic x86\\_64 with debian jessie sid \\\\ \\hline\n", | |
"notebook & 4.4.1 \\\\ \\hline\n", | |
"\\hline \\multicolumn{2}{|l|}{Tue Mar 21 15:33:55 2017 CDT} \\\\ \\hline\n", | |
"\\end{tabular}\n" | |
], | |
"text/plain": [ | |
"Software versions\n", | |
"Python 3.6.0 64bit [GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]\n", | |
"IPython 5.3.0\n", | |
"OS Linux 3.16.0 33 generic x86_64 with debian jessie sid\n", | |
"notebook 4.4.1\n", | |
"Tue Mar 21 15:33:55 2017 CDT" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%load_ext version_information\n", | |
"%version_information notebook" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Ref" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:20:32.421062Z", | |
"start_time": "2017-03-21T15:20:32.412919-05:00" | |
} | |
}, | |
"source": [ | |
"http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## code" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### init" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.453861Z", | |
"start_time": "2017-03-21T15:29:25.334409-05:00" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"!touch __init__.py" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### notebook_import.py" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.696559Z", | |
"start_time": "2017-03-21T15:29:25.458477-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# %load notebook_import.py\n", | |
"#!/usr/bin/env python\n", | |
"# Loads jupyter notebooks (.ipynb) as python modules (.py)\n", | |
"# source code: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html\n", | |
"\n", | |
"from IPython import get_ipython\n", | |
"from nbformat import read\n", | |
"from IPython.core.interactiveshell import InteractiveShell\n", | |
"import sys, io, types, os\n", | |
"import os.path\n", | |
"class NotebookLoader(object):\n", | |
" \"\"\"Module Loader for Jupyter Notebooks\"\"\"\n", | |
" def __init__(self, path=None):\n", | |
" self.shell = InteractiveShell.instance()\n", | |
" self.path = path\n", | |
" \n", | |
" @staticmethod\n", | |
" def find_notebook(fullname, path=None):\n", | |
" \"\"\"find a notebook, given its fully qualified name and an optional path\n", | |
"\n", | |
" This turns \"foo.bar\" into \"foo/bar.ipynb\"\n", | |
" and tries turning \"Foo_Bar\" into \"Foo Bar\" if Foo_Bar\n", | |
" does not exist.\n", | |
" \"\"\"\n", | |
" name = fullname.rsplit('.', 1)[-1]\n", | |
" if not path:\n", | |
" path = ['']\n", | |
" for d in path:\n", | |
" nb_path = os.path.join(d, name + \".ipynb\")\n", | |
" if os.path.isfile(nb_path):\n", | |
" return nb_path\n", | |
" # let import Notebook_Name find \"Notebook Name.ipynb\"\n", | |
" nb_path = nb_path.replace(\"_\", \" \")\n", | |
" if os.path.isfile(nb_path):\n", | |
" return nb_path \n", | |
"\n", | |
" def load_module(self, fullname):\n", | |
" \"\"\"import a notebook as a module\"\"\"\n", | |
" path = self.find_notebook(fullname, self.path)\n", | |
"\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", | |
" nb = read(f, 4)\n", | |
"\n", | |
"\n", | |
" # create the module and add it to sys.modules\n", | |
" # if name in sys.modules:\n", | |
" # return sys.modules[name]\n", | |
" mod = types.ModuleType(fullname)\n", | |
" mod.__file__ = path\n", | |
" mod.__loader__ = self\n", | |
" mod.__dict__['get_ipython'] = get_ipython\n", | |
" sys.modules[fullname] = mod\n", | |
"\n", | |
" # extra work to ensure that magics that would affect the user_ns\n", | |
" # actually affect the notebook module's ns\n", | |
" save_user_ns = self.shell.user_ns\n", | |
" self.shell.user_ns = mod.__dict__\n", | |
"\n", | |
" try:\n", | |
" for cell in nb.cells:\n", | |
" if cell.cell_type == 'code':\n", | |
" # transform the input to executable Python\n", | |
" code = self.shell.input_transformer_manager.transform_cell(cell.source)\n", | |
" # run the code in themodule\n", | |
" exec(code, mod.__dict__)\n", | |
" finally:\n", | |
" self.shell.user_ns = save_user_ns\n", | |
" return mod\n", | |
"\n", | |
"class NotebookFinder(NotebookLoader):\n", | |
" \"\"\"Module finder that locates Jupyter Notebooks\"\"\"\n", | |
" def __init__(self):\n", | |
" self.loaders = {}\n", | |
" \n", | |
" def find_module(self, fullname, path=None):\n", | |
" nb_path = self.find_notebook(fullname, path)\n", | |
" if not nb_path:\n", | |
" return\n", | |
"\n", | |
" key = path\n", | |
" if path:\n", | |
" # lists aren't hashable\n", | |
" key = os.path.sep.join(path)\n", | |
"\n", | |
" if key not in self.loaders:\n", | |
" self.loaders[key] = NotebookLoader(path)\n", | |
" return self.loaders[key]\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### nb/example_notebook.ipynb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.703890Z", | |
"start_time": "2017-03-21T15:29:25.699647-05:00" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import os.path\n", | |
"os.makedirs('nb', exist_ok=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.715446Z", | |
"start_time": "2017-03-21T15:29:25.707144-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Overwriting nb/example_notebook.ipynb\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile nb/example_notebook.ipynb\n", | |
"{\n", | |
" \"cells\": [\n", | |
" {\n", | |
" \"cell_type\": \"code\",\n", | |
" \"execution_count\": 2,\n", | |
" \"metadata\": {\n", | |
" \"ExecuteTime\": {\n", | |
" \"end_time\": \"2017-03-21T20:22:39.570288Z\",\n", | |
" \"start_time\": \"2017-03-21T15:22:39.564169-05:00\"\n", | |
" },\n", | |
" \"collapsed\": false,\n", | |
" \"run_control\": {\n", | |
" \"frozen\": false,\n", | |
" \"read_only\": false\n", | |
" }\n", | |
" },\n", | |
" \"outputs\": [],\n", | |
" \"source\": [\n", | |
" \"def foo():\\n\",\n", | |
" \" print(\\\"import worked!\\\")\"\n", | |
" ]\n", | |
" }\n", | |
" ],\n", | |
" \"metadata\": {\n", | |
" \"hide_input\": false,\n", | |
" \"kernelspec\": {\n", | |
" \"display_name\": \"pythonpath (python)\",\n", | |
" \"language\": \"python\",\n", | |
" \"name\": \"pythonpathkernel\"\n", | |
" },\n", | |
" \"language_info\": {\n", | |
" \"codemirror_mode\": {\n", | |
" \"name\": \"ipython\",\n", | |
" \"version\": 3\n", | |
" },\n", | |
" \"file_extension\": \".py\",\n", | |
" \"mimetype\": \"text/x-python\",\n", | |
" \"name\": \"python\",\n", | |
" \"nbconvert_exporter\": \"python\",\n", | |
" \"pygments_lexer\": \"ipython3\",\n", | |
" \"version\": \"3.6.0\"\n", | |
" },\n", | |
" \"latex_envs\": {\n", | |
" \"LaTeX_envs_menu_present\": true,\n", | |
" \"bibliofile\": \"biblio.bib\",\n", | |
" \"cite_by\": \"apalike\",\n", | |
" \"current_citInitial\": 1,\n", | |
" \"eqLabelWithNumbers\": true,\n", | |
" \"eqNumInitial\": 1,\n", | |
" \"labels_anchors\": false,\n", | |
" \"latex_user_defs\": false,\n", | |
" \"report_style_numbering\": false,\n", | |
" \"user_envs_cfg\": false\n", | |
" },\n", | |
" \"toc\": {\n", | |
" \"colors\": {\n", | |
" \"hover_highlight\": \"#DAA520\",\n", | |
" \"running_highlight\": \"#FF0000\",\n", | |
" \"selected_highlight\": \"#FFD700\"\n", | |
" },\n", | |
" \"moveMenuLeft\": true,\n", | |
" \"nav_menu\": {\n", | |
" \"height\": \"12px\",\n", | |
" \"width\": \"252px\"\n", | |
" },\n", | |
" \"navigate_menu\": true,\n", | |
" \"number_sections\": true,\n", | |
" \"sideBar\": true,\n", | |
" \"threshold\": 4,\n", | |
" \"toc_cell\": false,\n", | |
" \"toc_section_display\": \"block\",\n", | |
" \"toc_window_display\": false\n", | |
" }\n", | |
" },\n", | |
" \"nbformat\": 4,\n", | |
" \"nbformat_minor\": 2\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### usage" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.735859Z", | |
"start_time": "2017-03-21T15:29:25.718875-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"importing Jupyter notebook from /home/lewis/git/gist/ipynb_import_as_module/nb/example_notebook.ipynb\n" | |
] | |
} | |
], | |
"source": [ | |
"import sys, io, types, os.path\n", | |
"import notebook_import\n", | |
"# make docs/notebooks path findable\n", | |
"sys.meta_path.append(notebook_import.NotebookFinder())\n", | |
"\n", | |
"import importlib # around because notebook name starts with number \n", | |
"nb_dir = \"nb.\"\n", | |
"nb_name = \"example_notebook\"\n", | |
"mynotebook = importlib.import_module(nb_dir + nb_name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-03-21T20:29:25.743067Z", | |
"start_time": "2017-03-21T15:29:25.738451-05:00" | |
}, | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"import worked!\n" | |
] | |
} | |
], | |
"source": [ | |
"\n", | |
"mynotebook.foo()" | |
] | |
} | |
], | |
"metadata": { | |
"hide_input": false, | |
"kernelspec": { | |
"display_name": "pythonpath (python)", | |
"language": "python", | |
"name": "pythonpathkernel" | |
}, | |
"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.6.0" | |
}, | |
"latex_envs": { | |
"LaTeX_envs_menu_present": true, | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 1, | |
"labels_anchors": false, | |
"latex_user_defs": false, | |
"report_style_numbering": false, | |
"user_envs_cfg": false | |
}, | |
"toc": { | |
"colors": { | |
"hover_highlight": "#DAA520", | |
"running_highlight": "#FF0000", | |
"selected_highlight": "#FFD700" | |
}, | |
"moveMenuLeft": true, | |
"nav_menu": { | |
"height": "171px", | |
"width": "252px" | |
}, | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": 4, | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment