I have a Jupyter
notebook and I'd like to convert it into a Python
script using the nbconvert
command from within the Jupyter
notebook.
I have included the following line at the end of the notebook:
!jupyter nbconvert --to script <filename>.ipynb
This creates a Python
script. However, I'd like the resulting .py
file to have the following properties:
# In[27]:
# coding: utf-8
Ignore %magic
commands such as:
%matplotlib inline
!jupyter nbconvert --to script <filename>.ipynb
, i.e. the command within the notebook that executes the Python
conversionCurrently, the %magic
commands get translated to the form: get_ipython().magic(...)
, but these are not necessarily recognized in Python
.
Primarily, the nbconvert tool allows you to convert a Jupyter . ipynb notebook document file into another static format including HTML, LaTeX, PDF, Markdown, reStructuredText, and more. nbconvert can also add productivity to your workflow when used to execute notebooks programmatically.
Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.
Markdown cells can be selected in Jupyter Notebook by using the drop-down or also by the keyboard shortcut 'm/M' immediately after inserting a new cell.
You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key.
One way to get control of what appears in the output is to tag the cells that you don't want in the output and then use the TagRemovePreprocessor to remove the cells.
The code below also uses the exclude_markdown function in the TemplateExporter to remove markdown.
!jupyter nbconvert \
--TagRemovePreprocessor.enabled=True \
--TagRemovePreprocessor.remove_cell_tags="['parameters']" \
--TemplateExporter.exclude_markdown=True \
--to python "notebook_with_parameters_removed.ipynb"
To remove the commented lines and the input statement markets (like # [1]), I believe you'll need to post-process the Python file with something like the following in the cell after the cell you call !jupyter nbconvert from (note that this is Python 3 code):
import re
from pathlib import Path
filename = Path.cwd() / 'notebook_with_parameters_removed.py'
code_text = filename.read_text().split('\n')
lines = [line for line in code_text if len(line) == 0 or
(line[0] != '#' and 'get_ipython()' not in line)]
clean_code = '\n'.join(lines)
clean_code = re.sub(r'\n{2,}', '\n\n', clean_code)
filename.write_text(clean_code.strip())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With