Jupyter notebooks are an effective way to share research, ideas, steps, and other information to others. Sharing single notebooks or entire folders to collaborators is straightforward via GitHub but sharing the notebooks with the public through a blog post needs a few more steps (people read blogs not GitHub (unless they are software developers)). In the following we will outline the steps to embed Jupyter notebooks in a blog post.
Sharing non-interactive Jupyter notebook as static HTML
There are a few ways a non-interactive Jupyter notebook be shared as static HTML as long as they do not have interactive elements like pywidgets in them. They will still be exported as HTML but the interactive elements will not work. We will discuss embedding of interactive Jupyter notebooks in a later section.
Method I (simplest method)
- Upload file to Google Colab
- File -> Save a copy as GitHub Gist. It will ask for login permissions to GitHub first time ti is run.
- Go to GitHub. Then go to your Gists by clicking your profile pic at the upper right corner and selecting Your gists from the menu.
- Locate the Google Colab file just shared with Gist and open the Gist.
- Copy the Gist ID. For example, if the address bar shows https://gist.github.com/saugatach/100a28eb7dc353feb1ed3bf18f251443 then the Gist ID is 100a28eb7dc353feb1ed3bf18f251443.
- Go back to WordPress editor.
- In the WordPress post start a new paragraph.
- Edit as HTML.
- Add the line {gist]<Gist ID>[/gist]. For the Gist ID example above the line would be {gist]100a28eb7dc353feb1ed3bf18f251443[/gist]. Change the { before gist to a [. It is not a typo. WordPress will attempt to interpret the command if [ is used instead of {.
- Revert back to Edit visually. Wait for the Jupyter notebook to load with the Google Colab badge on it. If it doesn’t load within 30 seconds hit refresh.
Method II (slightly more hands-on)
- Export Jupyter notebook as a IPYNB file directly from JupterLab (or Jupyter interface).
- Open the IPYNB file in a text editor and copy the code.
- Go to GitHub and start a new Gist.
- Paste the IPYNB code in the code section.
- Manually name the file in Gist ending with .ipynb, for example, test.ipynb.
- Click edit at the top and after the page loads, change the privacy setting at the lower right corner of the code snippet to Public.
- Copy the Gist ID. For example, if the address bar shows https://gist.github.com/saugatach/100a28eb7dc353feb1ed3bf18f251443 then the Gist ID is 100a28eb7dc353feb1ed3bf18f251443
- Go back to WordPress editor.
- In the WordPress post start a new paragraph.
- Edit as HTML.
- Add the line {gist]<Gist ID>[/gist]. For the Gist ID example above the line would be {gist]100a28eb7dc353feb1ed3bf18f251443[/gist]. Change the { before gist to a [. It is not a typo. WordPress will attempt to interpret the command if [ is used instead of {.
- Revert back to Edit visually. Wait for the Jupyter notebook to load with the Google Colab badge on it. If it doesn’t load within 30 seconds hit refresh.
Method III (no extra frills, confusing steps)
- Export Jupyter notebook as a HTML file directly from JupterLab (or Jupyter interface).
- Open the HTML file in a text editor and copy the code.
- Go back to WordPress editor.
- In the WordPress post start a new paragraph.
- Edit as HTML.
- Paste the HTML code.
- Revert back to Edit visually. Wait for the Jupyter notebook to load. If it doesn’t load within 30 seconds hit refresh.
To access the HTML editor in WordPress
For older WordPress editor, there is a HTML editor tab. For the new WordPress editor, start a new paragraph, start typing random letters, stop after a few letters, move and hover the mouse over the text, a horizontal menu will appear, click on the 3 horizontal dots and select Edit a HTML. Clear the random letters, paste the HTML code from the Jupyter file, click on the 3 horizontal dots again, and select Edit visually.
I have used the method 1 and 2 above as described. But my preview is showing just a weblink. I even tried to embed by copying the embed code but it is not working. I tried the third method by copying all the HTML code of ipynb file, it shows the preview as intetactive jupyter notebook but in the actual blog after publishing I cannot find the notebook, I can only see the link. Can you please help?
LikeLike
The GitHub repository visibility might be set to Private.
LikeLike
Hi, I have done method III successfully but now I want to embed portions of an interactive google colab notebook. You said you were going to address this in a later section but I can’t find a blog post on it. Have you written on this topic as I’d love your guidance. Thanks!
LikeLike
That was awesome ! Thanks !
The last step didn’t work for me, but I inserted an HTML section, and used the `embed` tag from Gitlab, and it worked.
LikeLike