Convert ipynb notebook to HTML in Google Colab - python

I have a Google Colaboratory Notebook for Data Analysis that I want to output as a HTML file as currently not everything loads within the Colab environment such as large Folium Heatmaps. Is it possible to export the notebook as a html file as opposed to the ipynb and py options?

Method using Google Colab only
Download your .ipynb file
You can actually do it using only Google Colab. File -> Download .ipynb
Reupload it so Colab can see it
Click on the Files icon on the far left:
Then Upload to session storage:
Select & upload your .ipynb file you just downloaded.
Get your file's path
then obtain its path (you might need to hit the Refresh button before your file shows up):
Conversion using %%shell
Then, just as in Julio's answer, execute in a Colab cell:
%%shell
jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb
The %%shell lets the interpreter know that the following script is interpreted as shell. Don't write anything before %%shell, use a distinct cell for this.
The form of /PATH/TO/YOUR/NOTEBOOKFILE.ipynb will be something like /content/lightaberration3.ipynb.
Your file is ready
Might need to click Refresh again, but your notebook.html will appear in the files, so you can download it:
The great thing about this is that nothing python-related has to be installed on your computer, not conda, not pip, only a browser.

Google Colab doesn't currently have such a feature as a built-in.
Your best route is to first download it through File > Download .ipynb and then use the standard tool for Jupyter Notebook conversion, nbconvert:
jupyter nbconvert --to html notebook.ipynb
If you use an Anaconda Python distribution, nbconvert is most likely already installed. If not, refer to what is described in their install instructions to be able to convert:
pip install nbconvert
# OR
conda install nbconvert

to continue with "Method using only Google Colab"
" %%shell jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb" - as given
the following worked for me - type the following in Google Colab
!pip install nbconvert
%shell jupyter nbconvert --to html /content/testfile.ipynb
(instead of using %%shell in Google Colab, use %shell - this way, it worked for me)

Download your notebook and upload it back to the colab
Replace PATH_TO_THE_NOTEBOOK_IN_COLAB in the below command to the location of the reuploaded notebook.
Use this command to download the notebook as html => !jupyter nbconvert --to html PATH_TO_THE_NOTEBOOK_IN_COLAB.ipynb.

I tried the approach above but couldn't get it to work - for small jobs I log onto https://jupyter.org/try , upload the downloaded ipynb file from Google Colab and then I opened the file, from here you will have the functionality in jupyter.org to download as html or whatever other format you require. It took the pain out of trying to get it to work.

Related

.ipynb file not displaying properly in GitHub

I've uploaded a .ipynb jupyter notebook in GitHub but it doesn't display the exact same output as I've written in jupyter notebook.
Here's the link to my repo: ipynb file
You need to view the notebook as "rendered blob" not as "source blob". In GitHub, this is done by selecting the second rendering option in the top right:
It is mostly issue with Github backend. Try this way instead uploading directly
Go to --> https://nbviewer.org/
Paste your .ipynb github link here.
You can downlaod or embedded your file directly from here..

Jupyter download as PDF via LaTeX cutting off code at margins

New to Jupyter Notebook and trying to download the .ipynb file as a PDF via LaTex. I am trying to use pre-made templates so that the code in the cells aren't getting cut off at the margins when it converts to a PDF file. So far, I have tried two solutions (listed below) but running into errors, maybe someone can point me in the right direction?
Attempted Solution #1:
I followed the README in a pre-made template here: https://github.com/t-makaro/nb_pdf_template
Then in Jupyter Notebook: File > Download As > PDF via LaTex and get this error, see screenshot below. Am I placing the template files in the wrong folder and thus Jupyter Notebook is somehow not picking it up?
Attempted Solution #2:
I followed the steps in this guy's solution also:
https://www.markus-beuckelmann.de/blog/customizing-nbconvert-pdf.html
but then I get some error: ! LaTeX Error: File `tcolorbox.sty' not found.
I tried replicating the error following the steps at the GitHub linked on a Google Colab notebook. I've added the installation of additional libraries that are needed by nbconvert in order to unlock further capabilities.
!pip install nb_pdf_template
!python -m nb_pdf_template.install
!apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic
!jupyter nbconvert --to pdf ../testing.ipynb --template classic
This works without any problems on my end.

How to run GitHub project in a Jupyter Notebook?

I try to run this code in my own computer: https://github.com/ThaiPham27/Depression_Detection_DNN
I've downloaded the zip file but I have no idea how can I run this code in Jupyter notebook and get the output. I couldn't find any complete guide for that.\
I have Python 3.9 on Windows 10.
you can only run notebook file on Jupyter Notebook, and the extension of notebook files is ".ipynb"
And you need to have jupyter notebook on your system, or you can download Anaconda package for that.
P.S: I also briefly looked into the notebook, seems like it was setup for Google Colab as it is using some CSV from google drive.

View Jupyter notebooks hosted on Sharepoint

In our institute, we use OneDrive / Sharepoint from Microsoft to share files with each other. I am also sharing my Jupyter notebooks - with extension *.ipynb - there. Unfortunately, when one clicks on such a file the message "Hmm... looks like this file doesn't have a preview we can show you" appears. In my case I developed my notebooks with python. It's not necessary that my colleagues can run the notebook, but it would indeed be much easier to be able to install a kind of plugin for previewing on the Sharepoint server. Is there any product that can work like this? Otherwise I'll have to always export the file to HTML or so :-(
Unfortunately, there isn't any good news so far. Such a plugin seems to be unavailable as yet. For the meantime, I have automated the exporting of the notebooks to HTML on my Windows 10 machine as follows:
For /R .\ %G IN (*.ipynb) do jupyter nbconvert --to html "%G"
For use in a *.bat file, you'll have to double the %-signs.

How to export current notebook in HTML on Jupyter

How can I export the current notebook (from inside the notebook itself, using Python) to HTML (to a custom output path)?
By looking at nbconvert docs, you'll see that you can use --to html method:
import os
os.system('jupyter nbconvert --to html yourNotebook.ipynb')
This will create a yourNotebook.html file inside yourNotebook.ipynb folder.
You can do it. Use the following very simple steps:
I am using:
OS: Ubuntu, Anaconda-Jupyter notebook, Python 3
1 Save your Notebook in HTML format:
Start the jupyter notebook that you want to save in HTML format. First save the notebook properly so that HTML file will have a latest saved version of your code/notebook.
Run the following command from the notebook itself:
!jupyter nbconvert --to html your_notebook_name.ipynb
Edit: or you can also use:
!jupyter nbconvert your_notebook_name.ipynb --to html
After execution will create HTML version of your notebook and will save it in the current working directory. You will see one html file will be added into the current directory with your_notebook_name.html name
(your_notebook_name.ipynb --> your_notebook_name.html).
If you want to know how to save it into pdf format please check my answer on this question as well: IPython notebook - unable to export to pdf
Actually you need to save your notebook first, then you can use the nbconvert command
It would be something like:
%%javascript
IPython.notebook.save_notebook()
And Then
import os
os.system('jupyter nbconvert --to html yourNotebook.ipynb')
If you do not do this, it will not convert the actual notebook.
The above answer yields the result .ipynb to .html. But it exports .html file without output. In addition, if you want .html file with output, please use also --execute.
jupyter nbconvert --execute --to html notebook.ipynb

Categories

Resources