Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonyfast/importable
Import Jupyter notebooks and other files into Python
https://github.com/tonyfast/importable
import jupyter-notebook python
Last synced: 7 days ago
JSON representation
Import Jupyter notebooks and other files into Python
- Host: GitHub
- URL: https://github.com/tonyfast/importable
- Owner: tonyfast
- Created: 2017-06-28T03:38:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-05T04:08:07.000Z (over 7 years ago)
- Last Synced: 2025-01-10T15:15:03.948Z (about 1 month ago)
- Topics: import, jupyter-notebook, python
- Language: Jupyter Notebook
- Homepage:
- Size: 81.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.ipynb
Awesome Lists containing this project
README
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# importable \n",
"\n",
"`pip install git+`https://github.com/tonyfast/importable
\n",
"\n",
"> __importable__ is an IPython magic that permits python imports from notebook sources. [See the source.](https://github.com/tonyfast/importable/blob/master/importable.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
" %reload_ext importable "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"(42, , './readme.ipynb')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" foo = 42\n",
" import readme\n",
" readme.foo, readme.readme, readme.__file__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once __importable__ is loaded any notebook can be used as source. For example, [literacy](https://github.com/tonyfast/literacy) is a literate programming extension for the notebook [written with notebooks as source](https://github.com/tonyfast/literacy/blob/master/literacy/__init__.py#L1).\n",
"\n",
"__importable__ works in Python 2 and 3, reloading only works in python 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Installation\n",
"\n",
"`pip install git+`https://github.com/tonyfast/importable
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced: Custom Finder"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
" from importable import finder"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
" if __name__ == '__main__':\n",
" with open('test_file.json', 'w') as f: __import__('json').dump(['foo', 'bar'], f)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
" @finder('json')\n",
" def load_json(self, path):\n",
" \"\"\"Import files ending in json\"\"\"\n",
" return \"\"\"with open('{}') as f: data = __import__('json').load(f)\n",
" \"\"\".strip().format(self.path)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['foo', 'bar']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" import test_file\n",
" test_file.data"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook readme.ipynb to markdown\r\n",
"[NbConvertApp] Writing 2212 bytes to readme.md\r\n"
]
}
],
"source": [
" if True and __name__ == '__main__': \n",
" !jupyter nbconvert --to markdown readme.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"* http://brandonio21.com/2016/10/custom-fileextension-path-based-importers-in-python/\n",
"* https://chaobin.github.io/2015/06/22/understand-import-system-of-python/\n",
"* http://xion.org.pl/2012/05/06/hacking-python-imports/\n",
"* https://android.googlesource.com/toolchain/benchmark/+/master/python/src/Demo/imputil/importers.py\n",
"* https://docs.python.org/2.7/library/imputil.html\n",
"* PEPs: [302](https://www.python.org/dev/peps/pep-0302/), [420](https://www.python.org/dev/peps/pep-0420/), [451](https://www.python.org/dev/peps/pep-0451/)"
]
}
],
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}