An Activated AI utility for importing Jupyter notebooks as Python modules
Project description
ActivatedNotebookImporter
ActivatedNotebookImporter is a Python package developed and used internally by Activated AI (https://activated-ai.com) that allows you to import Jupyter notebooks as Python modules. This package is designed to seamlessly integrate Jupyter notebooks into your Python workflows.
Key Features
- Import Jupyter notebooks as Python modules
- Support for parameter substitution, enabling dynamic parameter setting
- Ability to exclude specific cells from execution using tags
- Automatic parameter detection from the first cell and cells tagged with "parameters"
Use Case
ActivatedNotebookImporter is particularly useful for workflows where you want to use Jupyter notebooks as reusable modules. Instead of converting your Jupyter notebooks to Python scripts, you can use them directly in your workflows:
- Set parameters using the
params
argument when importing the notebook - Exclude specific cells in the notebook by tagging them with 'import-exclude'
- Import the notebook as a module
- Call functions from the imported module with your desired parameters
- Get the results directly, without intermediate steps
This approach allows for more flexible and maintainable code, keeping your exploratory work and reusable code closely aligned.
Installation
You can install ActivatedNotebookImporter using pip:
pip install activated-notebook-importer
Usage
Here's a basic example of how to use ActivatedNotebookImporter:
from activated_notebook_importer import import_notebook
# Import a notebook with parameter substitution
module = import_notebook('path/to/your/notebook.ipynb',
{'param1': 'value1', 'param2': 'value2'})
# Assuming your notebook defines a 'my_function' function
result = module.my_function()
print(result)
In your Jupyter notebook:
# In the first cell or in cells tagged with "parameters"
param1 = 'default1'
param2 = 'default2'
# In a code cell
def my_function(param1=param1, param2=param2):
# Your code here
return some_result
# In another cell, tag this with 'import-exclude' in Jupyter
# This cell won't be executed when the notebook is imported
my_function()
Note:
- The first cell of the notebook is assumed to contain parameters, which can be overwritten with the
params
argument when importing. - You can also tag cells with the "parameters" tag for them to be considered as parameter cells.
Example: Hyperparameter Search
One potential use case is for hyperparameter search in machine learning models:
from activated_notebook_importer import import_notebook
hyperparameters = [
{'learning_rate': 0.01, 'batch_size': 32},
{'learning_rate': 0.001, 'batch_size': 64},
{'learning_rate': 0.0001, 'batch_size': 128}
]
results = []
for params in hyperparameters:
module = import_notebook('model_training.ipynb', params)
results.append(module.train())
# Process and analyze results
Internal Use at Activated AI
ActivatedNotebookImporter is a tool used in various workflows at Activated AI. We use it to:
- Integrate notebook-based code into larger systems
- Easily reuse and parameterize notebook code
- Maintain consistency between our exploratory notebooks and reusable code
By using this tool, we've improved our ability to leverage work done in Jupyter notebooks in our broader Python ecosystem.
Contributing
We welcome contributions! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
About Activated AI
ActivatedNotebookImporter is developed and maintained by Activated AI. Visit our website at https://activated-ai.com to learn more about our AI research and development.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file activated_notebook_importer-1.0.0.tar.gz
.
File metadata
- Download URL: activated_notebook_importer-1.0.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e46e306a5ad0ae92b1e1d51ef1c9b15522043f4820d79fdbb99af3f98a8d0ff |
|
MD5 | a97230236ed738234d7707ffe4a0be06 |
|
BLAKE2b-256 | e8fcc7d835229299db1e8bc82d3c97e9c649ee42b6223d185a51db344756dbd4 |
File details
Details for the file activated_notebook_importer-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: activated_notebook_importer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b34c26a23ae2cf26f0d010d0f795ff5e81bf967f18cc48c57cbf98adfcb0ed1 |
|
MD5 | ce4d29823eabc73eac9b21ed4f8c615b |
|
BLAKE2b-256 | 790ac94bcb6750d5e3e59315909aba69e53ec3d55514c4459b3fc651bc1142a0 |