I am trying to create slides for a data analysis task using jupyter notebook and nbconvert Python module.
I want to show the output of a cell which is the visualization part, and I want to get rid of the code part.
It is created but with the code part in the slide.
I tried a solution from Udacity Data Analysis Nano degree which suggests installing a pre-written template and then running the command:
jupyter nbconvert Example_Project_Diamonds_Part2.ipynb --to slides --post serve --template output_toggle
The template
but I get the error that 'slides_reveal.tpl' template does not exist, and when I try to download it from the internet I keep getting the same error but for different template names (It's like a vortex).
Open the Jupyter notebook Home page.
Choose New (on the top right) then Terminal.
Go to the directory of your .ipynb file.
Type the following command in the prompt:
jupyter nbconvert --to slides <filename>.ipynb --TemplateExporter.exclude_input=True
or you can just type in the jupyter notebook itself:
!jupyter nbconvert --to slides <filename>.ipynb --TemplateExporter.exclude_input=True
I would like to export my Jupyter notebook to html displaying the table of contents (toc2) AND hiding the input cells at the same time in the export.
I use a template to hide the code (https://github.com/ihuston/jupyter-hide-code-html), which works fine for itself.
For displaying the table of contents I run the command jupyter nbconvert --to html_toc FILE.ipynb, which works as well.
As soon as I try to combine both by jupyter nbconvert --to html_toc --template HIDE_CELL FILE.ipynb, only cells are hidden, but the toc is not shown anymore. Has anyone found a solution to combine both functions?
Thank you for your help!
I tried this command below and it hides the code and generates table of contents:
jupyter nbconvert /path/filename --to=html_toc --TemplateExporter.exclude_input=True
The above worked for: Ubuntu 18.10 and jupyter lab.
I'm looking for a way to hide code cells (inputs) when export my .iipynb file to a HTML. I don't want the code cells to be visible at all (not some button that turn them off/on). The output is for people that have no idea what a programming language is. I tried many things that I found on the internet but nothing seems to work.
Thanks
as of now (nbconvert version 5.6.0) the easiest solution seems to be to provide the argument --no-input when using the CLI interface of nbconvert:
jupyter nbconvert yourNotebook.ipynb --no-input
it works like magic
more info here
You can do this with an NBConvert template. Most of the examples out there are for latex/PDF, and won't work with HTML, which uses a different set of templates (and, for some reason, a different extension and slightly different file syntax).
Write the following into a template file called hidecode.tpl:
{%- extends 'full.tpl' -%}
{% block input_group %}
{%- if cell.metadata.get('nbconvert', {}).get('show_code', False) -%}
((( super() )))
{%- endif -%}
{% endblock input_group %}
Then convert your notebook to HTML with:
jupyter nbconvert --to html --template hidecode YourNotebook.ipynb
In recent versions of jupyter nbconvert you can use the --no-input option:
echo 'A Markdown cell with an equation $x=y+1$
```python
1 + 1
```
' | jupytext --to ipynb | jupyter nbconvert --stdin --execute --no-input --to html --output notebook.html
Now if you don't have the --no-input option, use --TemplateExporter.exclude_input=True, which is available from version 5.2.1 on.
I wrote an article about different ways how to hide code in Jupyter Notebook. According to my findings, there are several ways in which this can be done.
1. Hide all code in nbconvert
When exporting notebook with nbconvert you need to add --no-input in the command:
jupyter nbconvert --to html --no-input your-notebook.ipynb
2. Hide selected cells
You can hide selected cells by adding a tag hide_code, like in the animation below:
The command that hide code only for selected cells:
jupyter nbconvert --to html --TagRemovePreprocessor.remove_cell_tags='{"hide_code"}' my-notebook.ipynb
3. Export to HTML with Mercury
The Mercury is an open-source for sharing notebooks with non-technical users. It can hide code. It can also generate widgets for the notebook that are connected with variables in the code. The notebook export process is controlled with YAML header, below is the example YAML that hides the code:
---
title: My notebook
description: My amazing notebook with hidden code
show-code: False
---
The example notebook with YAML header:
The HTML (website) generated for the notebook with Mercury:
Complementing #vincentVega answer, you need to add the --to statement, otherwise it will throw the following error: ValueError: Please specify an output format with '--to <format>'.
jupyter nbconvert YourNotebook.ipynb --no-input --to html
Have your jupyter notbook ready
Go to Anaconda Prompt -> location of the jupyter notebook and enter the below command
jupyter nbconvert yourNotebook.ipynb --no-input --no-prompt
This will convert the Jupyter notebook to html with all the cells aligned to right.
I finaly found that : https://pypi.org/project/hide_code/0.2.0/
It's a jupyter extension and it's working like a charm. I use the command prompt to convert the notebook into an html, since the buttons that comes with the extension don't work for me.
For my use case I wanted to be able to create export without outputs from within a notebook, and with as little manual work as possible. An adequate solution I managed to glue together is as follows:
In first cell execute some javascript to get the name of the current notebook. The name will be stored in the nb_name variable which accessible within current python runtime scope.
Cell 1:
%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
In cell 2 pass the obtained name to a shell call and store a html with --no-input. The "!" in a jupyter notebook indicates a shell call and "&" is used to pass a variable from current python runtime scope to the shell call.
Cell 2:
print(nb_name)
!jupyter nbconvert --output-dir='./docs' --no-input --to html $nb_name
Conversion of ipynb code file to a HTML file without code(Using Python)::
Step1: Suppose your file Untitled.ipynb is saved in your laptop's Downloads folder.
Step2: Open Anaconda prompt or Cmd , Paste the below command to hide the codes and save the file as Untitled.html:
cd Downloads
jupyter nbconvert Untitled.ipynb --to=html --TemplateExporter.exclude_input=True
Note: path to the Untitled.ipynb could be different and needs to be changed where we are using cd Downloads to cd new_path.
I used nbinteract (https://www.nbinteract.com/) to publish the page and #HIDDEN (https://gitter.im/nbinteract/Lobby/) on top of the cell. It is undocumented and bound to change, but they'll keep it for backwards compatibility..
For others that might want to hide a specific code cell, one solution is to use a command line tool in nbdev package (developed by fastai) to export jupyter notebooks to markdown. The command is nbdev_nb2md.
When you do this, if you put #hide at the top of any notebook cell, it won't be exported to markdown. It will be ignored.
See this blog post for full details: https://www.fast.ai/2020/01/20/nb2md/
I am trying to get a practical example to use nbconvert.
I have seen a lot of blogs but I can get the idea right.
How do I select the folder where the Jupyter notebook is located and the destination folder for the HTML output? what is the right syntaxis to do it using Windows?
Thank you!!!
If you not afraid of command line then it must be as simple and comprehensive as this example:
ipython nbconvert --to html "C:\path\to\your_notebook.ipynb"
It will generate a HTML file named your_notebook.html at same place where your_notebook.ipynb is located.
If you need to take control over target HTML file generation you can ask ipython to output into standard output and redirect its output to whatever file you want.
Example:
ipython nbconvert --to html "C:\path\to\your_notebook.ipynb" --stdout > "C:\my\reports\fancy_analytic.html"
This will render your_notebook.ipynb in HTML format into fancy_analytics.html file.
More info here
The other answer is outdated, so I'm sharing the updated version:
Run this in the terminal:
pip install nbconvert
jupyter nbconvert notebook.ipynb --to html
nbconvert is very flexible, for example, you can convert to pdf:
jupyter nbconvert notebook.ipynb --to pdf
And there are flavors to each format. For example, to get a barebones HTML (no CSS):
jupyter nbconvert notebook.ipynb --to html --template basic
How do you convert an IPython notebook file (json with .ipynb extension) into a regular .py module?
From the notebook menu you can save the file directly as a python script. Go to the 'File' option of the menu, then select 'Download as' and there you would see a 'Python (.py)' option.
Another option would be to use nbconvert from the command line:
jupyter nbconvert --to script 'my-notebook.ipynb'
Have a look here.
According to https://ipython.org/ipython-doc/3/notebook/nbconvert.html you are looking for the nbconvert command with the --to script option.
ipython nbconvert notebook.ipynb --to script
In short: This command-line option converts mynotebook.ipynb to python code:
jupyter nbconvert mynotebook.ipynb --to python
note: this is different from above answer. ipython has been renamed to jupyter. the old executable name (ipython) is deprecated.
More details:
jupyter command-line has an nbconvert argument which helps convert notebook files (*.ipynb) to various other formats.
You could even convert it to any one of these formats using the same command but different --to option:
asciidoc
custom
html
latex. (Awesome if you want to paste code in conference/journal papers).
markdown
notebook
pdf
python
rst
script
slides. (Whooh! Convert to slides for easy presentation 😊)
the same command jupyter nbconvert --to latex mynotebook.ipynb
For more see jupyter nbconvert --help. There are extensive options to this. You could even to execute the code first before converting, different log-level options etc.
you can use this to do that :
pip install ipynb-py-convert
then run this on your terminal to convert .py file to .ipynb :
ipynb-py-convert Ex/abc.py Ex/abc.ipynb
to convert .ipynb files to .py :
ipynb-py-convert Ex/abc.ipynb Ex/abc.py
Tested on Ubuntu 20.04
Install required packages for PIP
$ pip install ipython
$ pip install nbconvert
To install required packages
$ sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic
METHOD 1
Use jupyter nbconvert command to convert to different format
Source file pro.ipynb
To convert to ascii
$ jupyter nbconvert --to asciidoc pro.ipynb
To convert to pdf
$ jupyter nbconvert --to pdf pro.ipynb
To convert to python
$ jupyter nbconvert --to python pro.ipynb
METHOD 2
Convert ipynb project through python code use savefig method of pyplot:
pro.ipynb
import matplotlib.pyplot as plt
%matplotlib inline
exp_vals=[1400,600,300,410,250]
exp_labels=['Home Rent','Food','Phone/Internet Bill','Car','Other Utilities']
plt.axis('equal')
plt.pie(exp_vals,labels=exp_labels,radius=2,autopct='%0.1f%%',shadow=True,explode=[0,0.5,0,0.3,0],startangle=20)
# plt.show()
plt.savefig('piechart.jpg',bbox_inches='tight',pad_inches=2,transparent=True,edgecolor='r')
piechart.png image that it generated:
Hope this helps to convert your ~(`)/\/\/\_ [Python] code
well first of all you need to install this package below:
sudo apt install ipython
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
two options are available either --to python or --to=python
for me this works fine:
jupyter nbconvert --to python while.ipynb
[NbConvertApp] Converting notebook while.ipynb to python
[NbConvertApp] Writing 758 bytes to while.py
pip3 install ipython
if it does not work for you try, by pip3.
pip3 install ipython
You definitely can achieve that with nbconvert using the following command:
jupyter nbconvert --to python while.ipynb
However, having used it personally I would advise against it for several reasons:
It's one thing to be able to convert to simple Python code and another to have all the right abstractions, classes access and methods set up. If the whole point of you converting your notebook code to Python is getting to a state where your code and notebooks are maintainable for the long run, then nbconvert alone will not suffice. The only way to do that is by manually going through the codebase.
Notebooks inherently promote writing code which is not maintainable (https://docs.google.com/presentation/d/1n2RlMdmv1p25Xy5thJUhkKGvjtV-dkAIsUXP-AL4ffI/edit#slide=id.g3d7fe085e7_0_21). Using nbconvert on top might just prove to be a bandaid. Specific examples of where it promotes not-so-maintainable code are imports might be sprayed throughout, hard coded paths are not in one simple place to view, class abstractions might not be present, etc.
nbconvert still mixes execution code and library code.
Comments are still not present (probably were not in the notebook).
There is still a lack of unit tests etc.
So to summarize, there is not good way to out of the box convert python notebooks to maintainable, robust python modularized code, the only way is to manually do surgery.
You can run a .py file in the same directory:
import json
files = ["my_first_file_name.ipynb", "my_second_file_name.ipynb"]
for file in files:
code = json.load(open(file))
py_file = open(f"{file}.py", "w+")
for cell in code['cells']:
if cell['cell_type'] == 'code':
for line in cell['source']:
py_file.write(line)
py_file.write("\n")
elif cell['cell_type'] == 'markdown':
py_file.write("\n")
for line in cell['source']:
if line and line[0] == "#":
py_file.write(line)
py_file.write("\n")
py_file.close()
I rewrite this code from Syrtis Major's answer.
You can use the following script to convert jupyter notebook to Python script, or view the code directly.
To do this, write the following contents into a file cat_ipynb, then chmod +x cat_ipynb.
#!/usr/bin/env python
import sys
import json
for file in sys.argv[1:]:
print('# file: %s' % file)
print('# vi: filetype=python')
print('')
code = json.load(open(file))
for cell in code['cells']:
if cell['cell_type'] == 'code':
print('# -------- code --------')
for line in cell['source']:
print(line, end='')
print('\n')
elif cell['cell_type'] == 'markdown':
print('# -------- markdown --------')
for line in cell['source']:
print("#", line, end='')
print('\n')
Then you can use
cat_ipynb your_notebook.ipynb > output.py
Or show it with vi directly
cat_ipynb your_notebook.ipynb | view -
Go to https://jupyter.org/
click on nbviewer
Enter the location of your file and render it.
Click on view as code (shown as < />)
One way to do that would be to upload your script on Colab and download it in .py format from File -> Download .py
Convert the Ipynb dir files to .py
import os
for fname in os.listdir():
if fname.endswith('ipynb'):
os.system(f'jupyter nbconvert {fname} --to python')
Jupytext allows for such a conversion on the command line, and importantly you can go back again from the script to a notebook (even an executed notebook). See here.
Copy all the (''.ipynb) files in the Desired folder then execute:
import os
desired_path = 'C:\\Users\\Docs\\Ipynb Covertor'
os.chdir(desired_path)
list_of_directory = os.listdir(desired_path)
for file in list_of_directory:
os.system('ipython nbconvert --to script ' + str(file))
If this is a one-off, follow e.g. #kikocorreoso depending if you want to use command line or gui.
However, if you want some solution that will maintain a synchronized version of the .py and the .ipynb you really should consider using jupytext as also pointed out by #Wayne
Run conda install jupytext or pip install jupytext
Then do:
jupytext --set-formats ipynb,py <file>.ipynb
To keep it synchronized to the .py file:
jupytext --set-formats ipynb,py <file>.ipynb --sync
This will make sure jupyter keeps the two files in sync when saving from now on...
Last note: If you are a gui person, after running the installation command for jupytext, everything else can be done from the gui as well File-->Jupytext-->pair Notebook with light Script:
jupyter nbconvert [filename].ipynb --no-prompt --to python
The above code will generate [filename].py in the same directory
My version of Jupyter Notebook, 3.3.2, has this option on the File Menu: