Skip to main content

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.

Source Distribution

nbtof-0.0.8.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbtof-0.0.8-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file nbtof-0.0.8.tar.gz.

File metadata

  • Download URL: nbtof-0.0.8.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for nbtof-0.0.8.tar.gz
Algorithm Hash digest
SHA256 dd6515fb8b72992938ea2fc5531a488ee32faf477590a198fda0d2e0a81d505a
MD5 c8dae6153e2877379f96ada318e52818
BLAKE2b-256 ddcffef89f41d731dbebe72a21e1ca1b0ead8778938e442b721d23cd0a097db6

See more details on using hashes here.

File details

Details for the file nbtof-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: nbtof-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for nbtof-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d859141c5d252551f390455b71e6424dba9c8cc9c6c5bda0b1f560e9161df881
MD5 2aee13d597d92448381ceb683f418351
BLAKE2b-256 7bea00667e3e538233f3440f7c5d5f2c27d0beaae83d25a7a8891e4824486db3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page