Sharing and Exporting Notebooks
# CHAPTER 16
Sharing and Exporting Notebooks
1. Chapter Introduction
You have built a beautiful notebook. The data is clean, the machine learning model is trained, and the Matplotlib charts look amazing. Now, you need to send it to your manager. If you email them the raw.ipynb file, they won't be able to open it unless they also have Python and Jupyter installed. This chapter covers how to export notebooks into universally readable formats like HTML and PDF, and how to share them online.
2. Exporting to HTML
The easiest and most reliable way to share a static view of your notebook is to export it as an HTML file. An HTML file can be opened in any web browser (Chrome, Edge, Safari) by anyone, without needing Python installed.
How to export to HTML:
- 1. In the Jupyter menu, go to File -> Download as.
- 2. Select HTML (.html).
- 3. The file will download to your computer.
*Why HTML?* It perfectly preserves your Markdown formatting, your code blocks, and your charts exactly as they look in Jupyter.
3. Exporting to PDF
PDFs are the standard for formal business reports and academic papers.
How to export to PDF (The Easy Way):
- 1. First, export the notebook to HTML.
- 2. Open the downloaded HTML file in Google Chrome.
-
3.
Press
Ctrl + P(orCmd + P) to open the Print dialog.
- 4. Set the destination to "Save as PDF".
How to export to PDF (The Native/LaTeX Way): Jupyter has a File -> Download as -> PDF via LaTeX option. However, this requires you to have a massive LaTeX engine (like MiKTeX or MacTeX) installed on your operating system, and it often breaks if your notebook contains complex widgets. The Chrome HTML-to-PDF trick is universally preferred by professionals for its simplicity.
4. Sharing via GitHub
If you are building a data science portfolio to get a job, you must put your notebooks on GitHub. GitHub natively supports .ipynb files!
The Workflow:
- 1. Create a free account on GitHub.
- 2. Create a new Repository.
-
3.
Upload your
.ipynbfile to the repository.
-
4.
When someone clicks on the
.ipynbfile in GitHub, GitHub automatically renders the notebook. They can read your Markdown, see your code, and view your charts directly on the GitHub website.
*Note:* GitHub rendering is static. Viewers cannot run the code.
5. Jupyter nbviewer
Sometimes GitHub's internal notebook renderer fails on very large notebooks. The official Jupyter project created nbviewer to solve this.
- 1. Go to nbviewer.jupyter.org.
-
2.
Paste the URL of your GitHub notebook (or any publicly hosted
.ipynbfile).
- 3. nbviewer will render it beautifully and incredibly fast. It even provides a link for users to download it.
6. Executable Sharing (Google Colab / Binder)
What if you want someone to be able to *run* your code, but they don't have Python installed?
1. Google Colab:
Google provides a free, cloud-based Jupyter Notebook environment. You can upload your .ipynb file to your Google Drive, right-click it, and select "Open with Google Colaboratory". You can then share the link just like a Google Doc. Anyone with the link can run the Python code using Google's cloud servers.
2. Binder (mybinder.org): You can paste a link to your GitHub repository into Binder. Binder creates a temporary, interactive cloud environment for your repository. Users can click the link, and a live Jupyter Notebook will open in their browser, allowing them to run and edit the code without installing anything.
7. Mini Project: Publish a Data Science Report
The Task:
- 1. Open any completed notebook you have.
- 2. Click Kernel -> Restart & Run All to ensure it runs cleanly.
- 3. Click File -> Download as -> HTML.
- 4. Open the HTML file in Chrome, click Print, and save it as a PDF.
- 5. Email the PDF to a friend or colleague. You have just published a reproducible report!
8. Common Mistakes
-
Sharing
.ipynbfiles with non-technical people: Never email a.ipynbfile to a business stakeholder. When they try to open it, their computer won't know what application to use, and if they force it open in Notepad, they will see terrifying JSON code. Always send HTML or PDF.
- Exporting without re-running: If you made changes to your code but didn't run the cells, the exported HTML will show the *old* output. Always "Restart & Run All" before exporting.
9. MCQs
If you want to email a static version of your notebook to a manager who does NOT have Python, what is the best format?
How do you export a notebook to HTML?
What is a common, easy way to convert a notebook to a PDF without installing LaTeX?
Does GitHub natively support viewing Jupyter Notebooks?
Can a user *run* the Python code inside a notebook that is hosted on GitHub?
What tool creates a live, interactive cloud environment from a GitHub repository so anyone can run the code?
What is Google Colab?
What should you ALWAYS do immediately before exporting a notebook?
If GitHub fails to render a very large notebook, what official Jupyter tool should you use?
Why is emailing a raw .ipynb file to a non-technical person a bad idea?
10. Interview Questions
- Q: How do you ensure that a Jupyter Notebook is fully reproducible before you share it on your GitHub portfolio?
- Q: Your manager needs an interactive dashboard where they can run code, but they refuse to install Python on their laptop. What cloud-based solution do you propose?
11. Summary
Sharing your work is just as important as writing the code. For static sharing (just reading), export your notebook to HTML or PDF. If you are building a portfolio, upload your.ipynb files to GitHub, which automatically renders them. If you need stakeholders to *run* the code without installing Python, use cloud platforms like Google Colab or Binder. Always remember to "Restart & Run All" before sharing!