I’m rounding that corner of my research where the end seems to be in sight–which also means that properly formatting my work for publication can’t be put off too much longer. Depending what I’m coding, I’m either in PyCharm, Sublime, or Jupyter. My dissertation is, of course, in LaTex(…as well on my whiteboard, scattered printer paper, post its, long forgotten notebooks, and the margins of so many journal papers). If my work is already in a script, then getting it to LaTex is a snap. But if it’s written in Jupyter, it needs a little love before it’s fit for prime-time.
From Script to Latex
There’s a nice little package for LaTex that imports your python script (
your_script.py) and presents it neatly
and you can include your script via
It will appear nicely formatted with appropriate bolding, etc.
That’s great if your scripts already looks nice. And sure, some of mine do. But I’ve built a few simulations in Jupyter notebook.
From Jupyter Notebook to Script
There is a convenient means to create a python executable from your notebook. You can run this in your terminal
jupyter nbconvert --to script path/to/your_notebook.ipynb
This will produce a
your_notebook.py executable. It’s fine if you just want to run it, but it’s not all that great to look at. It includes cell numbers, awkward spacing, and just looks a bit messy. It’s not what I want in my dissertation. And I’m certainly not going to open up each file and tidy them up manually–especially not every time I tweak and edit my code. I know I will eventually forget to export and tidy the updated script. So, let’s just make it mindless.
From Jupyter Notebook to Script-Fit-For-Latex
Using a little magic(!) and a bit of processing, I added these lines to the end of my notebooks. (Don’t worry–I’ve also added the code below for copy-paste purposes.)
NOTE: The first cell is markdown (the rest are python cells):
# DO NOT ADD BELOW TO SCRIPT
The next two cells are magic-ed (the ‘!’ at the beginning). The first converts
this_notebook.ipynb (the actual name of the notebook you’re currently in) to
! jupyter nbconvert --to script this_notebook.ipynb
The next cell is optional, but there’s a good chance you want it for organizational purposes (especially if, as I recommend, you are versioning your work via github/gitlab/etc). Also, if your notebook takes a long time to run, consider copying (
cp) instead of moving (
mv) your file. This will come up at the end.
! mv this_notebook.py your/desired/location/this_notebook.py
The next cell will rewrite your script in place, but leave out the extraneous stuff. NOTE: you will have to update
import re f_name = "your/desired/location/this_notebook.py" do_not_add_below_to_script = "# DO NOT ADD BELOW TO SCRIPT" # must match the markdown above! skip = 0 cell_nums = re.escape("# In[") + r"[0-9]*" + re.escape("]:") with open(f_name, "r") as f: lines = f.readlines() # get a list of lines from the converted script with open(f_name, "w") as f: # overwrite the original converted script for line in lines: if re.search(cell_nums , line.strip()): # don't include the '#In[##]:' lines skip = 2 elif skip > 0 and line == "\n": # trim extra blank lines below #In[##]:' lines skip -=1 elif re.search(do_not_add_below_to_script, line): # don't include this code break else: f.write(line)
Make sure to update you LaTex path as well
If the Script isn’t updating… Don’t Worry!!!
It just means nb convert is looking at a previous version (annoying, I know). In Jupyter Notebook, just go to File –> Save & Checkpoint. Then rerun this code.
If LaTex isn’t updating… Don’t Worry!!!
LaTex if finicky. We all know that. Fortunately, when an imported file isn’t updating the fix is usually the same.
your/desired/location/this_notebook.py file. Rebuild your LaTex file so that you get an error (or just delete the appropriate build file). Then rerun the code I’ve shown here — but if you chose just to copy your file, run only that cell and below.
Hope this helps! I look forward to seeing your beautiful code in a publication somewhere soon!