nbtof: transfering notebook to function
Project description
nbtof
| English | 日本語 |
This module is used to convert Jupyter Notebook to the python function with the same process.
Introduction
Only by writing the #@~
marks in Jupyter Notebook file, you can easily convert Jupyter Notebook file to the python source file with the function which perform the same process as the Jupyter Notebook file. For example, in the case that you want to convert sample.ipynb
to function,
sample.ipynb
#@param
a = 1
b = 1
c = a + b
#@return
c
you can get sample.py
by executing the next code.
import nbtof
nbtof.nbtof_generate(
notebook_name='sample.ipynb',
func_py_name='sample.py',
)
sample.py
def sample(a, b):
c = a + b
return c
Installation
The latest nbtof can be installed from PyPI:
$ pip install nbtof
Documentation
Marks list
Mark | Description |
---|---|
#@param |
Variable names in the cell become function's argument names. The values assigned at the Jupyter Notebook is ignored. |
#@default |
Variable names in the cell become function's argument names. The values assigned at the Jupyter Notebook get default values. |
#@args |
Variable names in the cell become function's variable length argument *args names. The values assigned at the notebook is ignored. |
#@kwargs |
Variable names in the cell become function's variable length argument **kwargs names. The values assigned at the notebook is ignored. |
#@return |
The line after this mark become function's return value |
#@ignore |
Anything in the cell is ignored. |
#@help |
What you write in the cell becomes function's docstring. Please write it in quotation marks. |
#@advance |
What you write in the cell is written before the function declaration as it is. (e.g., imports) |
#@r_advance |
The comment line with # in the cell is written with the # removed before the function declaration. Please use to avoid the error in Jupyter Notebook (e.g., relative imports) |
Details about marks
#@param
The Jupyter Notebook
sample_param.ipynb
#@param
a = 0
print("Hello World !")
is converted into
def sample_param(a):
print('Hello world !')
#@default
The Jupyter Notebook
sample_default.ipynb
#@default
a = 0
print("Hello World !")
is converted into
def sample_default(a=0):
print('Hello world !')
#@args
sample_args.ipynb
#@args
a = 0
print("Hello World !")
is converted into
def sample_args(*a):
print('Hello world !')
#@kwargs
sample_kwargs.ipynb
#@kwargs
a = 0
print("Hello World !")
is converted into
def sample_kwargs(**a):
print('Hello world !')
#@return
sample_return.ipynb
#@return
a = 0
if a == 0:
#@return
True
else:
#@return
False
is converted into
def sample_return(a):
if a == 0:
return True
else:
return False
#@ignore
sample_ignore.ipynb
#@ignore
1 + 1
print('Hello world !')
is converted into
def sample_ignore():
print('Hello world !')
#@help
sample_help.ipynb
#@help
"""
This function outputs "Hello world !" sentence.
Parameters
----------
Returns
-------
"""
print('Hello world !')
is converted into
def sample_help():
"""
This function outputs "Hello world !" sentence.
Parameters
----------
Returns
-------
"""
print('Hello world !')
#@advance
sample_advance.ipynb
#@advance
import os
import sys
#@advance
def func():
print(os.getcwd())
print(sys.prefix)
print("Hello world !")
func()
is converted into
import os
import sys
def func():
print(os.getcwd())
print(sys.prefix)
print('Hello world !')
def sample_advance():
func()
#@r_advance
sample_r_advance.ipynb
#@r_advance
#from . import foo
#from . import bar
print("Hello world !")
is converted into
from . import foo
from . import bar
def sample_r_advance():
print('Hello world !')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.