After we created our analysis, it is time to share our Jupyter notebooks. Let us look at the options we have.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Share the *.ipynb file
We can save our Jupyter Notebook to a *.ipynb file, either by using the File / Download menu entry or by copying the file directly form the file system:
You can share this file your colleagues and they can use the menu entry File / Open from Path… to open the notebook in their JupyterLab instance:
Various other tools like VS Code or PyCharm allow you to open a *.ipynb
file as well and offer a similar experience to work with the notebook as you know from JupyterLab.
Share via GitHub
If you share your notebook as a *.ipynb file via GitHub, you get a preview of the notebook directly in the web view:
If your colleagues only need a read-only view, then the GitHub preview may be enough. And if you work with others on a notebook, Git (and GitHub) makes it much simpler to share the changes.
Export to various formats
In the JupyterLab application is a menu item File / Save and Export Notebook As… with these formats:
The Markdown and HTML format work out of the box with no additional packages. The other formats require Pandoc and various other packages to work. I suggest you only install what you really need. Otherwise you fill your system with many packages that will be no use to you.
A little tip for Pandoc: Pandoc is an executable that will only be available in your console and JupyterLab after you reinitialised the console. Therefore, you best start a new console to run JupyterLab with your freshly installed Pandoc.
Export to Python (Jupyter Notebook only)
If you run the old Jupyter Notebook application, you get the option to download your notebook as a Python file in the menu File / Download as / Python (.py):
This merges all your cells into a single Python file and comments out the Markdown cells:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#!/usr/bin/env python # coding: utf-8 # Small code snippets go directly in the notebook # In[1]: def add(a, b): return a + b # In[2]: add(1, 2) # Larger code goes into a regular Python file # In[3]: import os, sys code_dir = os.path.abspath(os.path.join(os.getcwd(), 'code')) sys.path.insert(0, code_dir) # In[4]: from fizzbuzz import fizz_buzz # In[5]: fizz_buzz(18) # We can load our data from outside the notebook # In[6]: #import os data_file = os.path.abspath(os.path.join(os.getcwd(), 'data', 'project_size.csv')) # In[7]: import pandas as pd df = pd.read_csv(data_file, delimiter=';') # In[8]: df # In[ ]: |
While the usefulness of this export format is debatable, I think it is good to know that it exists.
Online services
To complete the list I have a few words on online services. There are numerous offerings that allow you to host your notebooks online. Depending on the business model of those platforms, you may need to pay more or less to get an easy way to share your work. So far, I do not have enough experience to make a suggestion on where to host your notebooks. If you can fill the blanks, please leave a comment with your suggestions.
Next
Now that we can share our Jupyter Notebooks, we have everything together to do some interesting work. Starting next week, we look at Matplotlib and how we can turn boring data into useful visualisations.
1 thought on “Python Friday #163: Share Your Jupyter Notebooks”