<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Importing Jupyter Notebooks as "Objects"
Jupyter Notebooks are great for data exploration, visualizing, documenting, prototyping and iteracting with the code, but when it comes to creating an actual program out of a notebook they fall short. I often get myself copying cells from a notebook into a script so that I can run the code with command line arguments. There is no easy way to run a notebook and return a result from its execution, passing arguments to a notebook and running individual code cells programatically. Have you ever wrapped a code cell to a function just so you want to call it in a loop with different paramethers?
I wrote a small utility tool nbloader that enables code reusing from jupyter notebooks. With it you can import a notebook as an object, pass variables to it's name space, run code cells and pull out variables from its name space.
This tutorial will show you how to make your notebooks resusable with nbloader.
Instal nbloader with pip
shell
pip install nbloader --upgrade
Load a Notebook
Step1:  The above commad loades a notebook as an object. This can be done inside a jupyter notebook or a regular python script.
Run all cells
Step2:  After loaded_notebook.run_all() is called
Step3:  The notebooks namesace is just a dict so if you try to get something thats not there will get an error.
Step4:  Run individual cells if they are tagged.
Step5:  If a cell have a comment on its first line it will become a tag.
This is how you mess with its namespace
<ASSISTANT_TASK:>
Python Code:
from nbloader import Notebook
loaded_notebook = Notebook('test.ipynb')
Explanation: Importing Jupyter Notebooks as "Objects"
Jupyter Notebooks are great for data exploration, visualizing, documenting, prototyping and iteracting with the code, but when it comes to creating an actual program out of a notebook they fall short. I often get myself copying cells from a notebook into a script so that I can run the code with command line arguments. There is no easy way to run a notebook and return a result from its execution, passing arguments to a notebook and running individual code cells programatically. Have you ever wrapped a code cell to a function just so you want to call it in a loop with different paramethers?
I wrote a small utility tool nbloader that enables code reusing from jupyter notebooks. With it you can import a notebook as an object, pass variables to it's name space, run code cells and pull out variables from its name space.
This tutorial will show you how to make your notebooks resusable with nbloader.
Instal nbloader with pip
shell
pip install nbloader --upgrade
Load a Notebook
End of explanation
loaded_notebook.run_all()
Explanation: The above commad loades a notebook as an object. This can be done inside a jupyter notebook or a regular python script.
Run all cells
End of explanation
loaded_notebook.ns['a']
Explanation: After loaded_notebook.run_all() is called:
- The notebook is initialized with empty starting namespace.
- All cells of the loaded notebook are executed one after another by the order they are the file.
- All print statement or any other stdout from the loaded notebook will output.
- All warnings or errors will be raised unless catched.
- All variables from the loaded notebook's namespace will be accesable.
Here is the contents of loaded_notebook.ipynb
<img src="tests/loaded_notebook.png" width="400">
This is how you access the namespace of the loaded notebook
End of explanation
loaded_notebook.ns['b']
Explanation: The notebooks namesace is just a dict so if you try to get something thats not there will get an error.
End of explanation
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
Explanation: Run individual cells if they are tagged.
End of explanation
loaded_notebook.ns['a'] = 0
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
Explanation: If a cell have a comment on its first line it will become a tag.
This is how you mess with its namespace
End of explanation
<END_TASK>