Skip to content

Converts a .py python source file to a V.4 .ipynb jupyter/ipython notebook.

License

Notifications You must be signed in to change notification settings

tobycheese/py2ipynb

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py2ipynb

Converts a .py python source file to a V.4 .ipynb jupyter/ipython notebook.

Based on this and this posts. Question originally asked on stackoverflow.

Usage

Run from the examples folder, and don't forget to add the py2ipynb path to your system path.

Default:

py2ipynb.py example_default.py example_default.ipynb

Pycharm style:

py2ipynb.py example_pycharmlike.py example_from_pycharmlike.ipynb -c pycharm

Spyder style:

py2ipynb.py example_spyderlike.py example_from_spyderlike.ipynb -c spyder

Cell Markers

Default style

For default style, see the source code (look at the if/elif line.startswith statements).

From this, it seems that generally the markers are # <codecell>, # <markdowncell, # <rawcell>, and the deprecated in V.4 # <htmlcell> and # <headingcell.

Pycharm/Spyder style

Code cell:

  • pycharm style: ##
  • spyder style: #%%

Markdown cell:

Use one of the code cell markers first, then in a newline follow by #md (also # md, #markdown, # markdown), and include your markdown code within a block comment below. Everything till the next cell mark (e.g. ## if using pycharm style cell markers) will go to a markdown cell.

E.g.

##
# md
"""
# Header

* item 1
* item 2
"""

##
print("hello world")

Inline matplotlib

Simply add in a new line after the cell marker #%matplotlib inline or # %matplotlib inline, or instead of inline any other supported backend.

Slide metadata

Add after the cell marker in a new line an optional slide metadata marker #slide: <slide_type> (also # slide: <slide_type>), where <slide_type> can be one of the standard slide types -, slide, sub-slide, fragment, skip and notes.

Default when there is no slide tag is slide.

E.g.

##
```python
##
# md
# slide:skip
"""
# Header

* item 1
* item 2
"""

##
#slide:slide
print("hello world")

On how to convert the notebook to a static format (including slides format) read this doc-page, or specifically for slides generation this guide is helpful.

Examples

See also examples: default | pycharm-style | spyder-style

About

Converts a .py python source file to a V.4 .ipynb jupyter/ipython notebook.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%