Recently I was working on a project where I needed to convert a bunch of Jupyter notebooks to another format, specifically the .py Python script.
Now, converting just one Jupyter notebook is a cinch and can be done through the interface: Go to File > Download as. We’ll stick with the .py
Python script file extension:
But, as our Grumpy Orthodox Cat tells us, if we’ve got more than two or three notebooks to convert (no pun intended), we should really consider automating all that boring stuff.
We can do that, but it’ll take a wee bit of (gasp!) work at the command line.
Setup
I’ll be walking through this on a free download of Anaconda Individual on a Windows computer. A repo of Jupyter notebooks is available at GitHub. You can either fork this repo or download and decompress it to your computer.
If you’re not on this same setup I’ll be less able to answer any questions you may have, sorry!
1. Navigate to your folder of files
With everything ready to go, the first step is to find the file path on your computer where these files are located. You can browse through File Explorer to do that. When you are done, you will want to copy the location of that path to your clipboard. Here’s a cool tip for doing that: find the desired folder, then click on the folder name to copy/paste it (You’ll be pasting it into Anaconda Navigator in the next step):
Set cd
for current directory
So where exactly is this file path going? You’ll put it in the Anaconda Prompt, which is a command-line tool for PCs to work with Anaconda. You can open it from the Windows menu.
When you get there, you’ll want to set the current directory of your session to wherever your Jupyter notebooks are… which is where you just copied… This is basically telling Anconda Prompt, “This is my home base for where I want to do my work.”
To do this, you’ll type cd
at the blinking cursor, then paste in your file path (Ctrl + V should work just fine):
Convert the files in that folder
Survived the command prompt so far? Nice, but I have to tell you — there’s a wildcard coming up…
The wildcard operator
Here’s the situation: We want to convert all of the Jupyter files in our folder to a Python script. We need a way to communicate that to the computer. And the way we can do that is with the wildcard operator, *
.
This is not unique to Python or Anaconda; you will see *
used in SQL too, for example. In general, a wild card symbol is used to replace or represent one or more characters. Particularly, the asterisk is used to stand in for anything.
So, we are going to use Jupyter’s nbconvert
tool to convert a certain .ipynb file in our folder (and we specified that folder by using cd
earlier) to Python. When we use *
as the name of that file, the wild card represents every Jupyter notebook in the folder:
Take a look at it in action:
What if I want another format?
Good news — this little hack will work for Markdown files, HTML… you name it. Just check the documentation to find the right code to insert instead of Python
.
I hope that this saved you a bit of conversion time. Perhaps you learned a bit about wild cards and the command line in the process.
Let me know in the comments — what else in Python are you currently working on automating?
Leave a Reply