Originally published at http://www.ds4n6.io.

jupyter nbconvert my_notebook.ipynb --to pdf

If you want to change the font size, for instance, you can do it through overriding templates ( docs, base templates), but it can get complex. So the easiest way is to convert to LaTeX first, do some simple replacements, and then convert the resulting LaTeX to pdf via the pdflatex utility:

$ jupyter nbconvert --to latex my_notebook.ipynb
$ sed -r -i 's/documentclass\[11pt\]\{article\}/documentclass[8pt]{extarticle}/' my_notebook.tex # <---- This modifies the font size from 11pt to 8pt
$ sed -r -i 's/geometry\{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}/geometry{verbose,tmargin=0.5in,bmargin=0.5in,lmargin=0.2in,rmargin=0.2in}/' my_notebook.tex # <---- This sets the margins
$ pdflatex my_notebook
$ rm my_notebook.tex; rm my_notebook.out; rm my_notebook.aux; rm Requirements:

Requirements:

  • texlive-xetex must be installed in the system (Ubuntu installation: apt-get install texlive-xetex texlive-generic-extra)

Refs:

--

--

Originally published at http://www.ds4n6.io.

import nbformat as nbfnb = nbf.v4.new_notebook()# Text Cell ---------------------------------
text = """\
# My first automatic Jupyter Notebook
This is an auto-generated notebook."""
# Code Cell ---------------------------------
code = """\
%pylab inline
hist(normal(size=2000), bins=50);"""
# Create & Save the Notebook ----------------
nb['cells'] = [nbf.v4.new_markdown_cell(text), nbf.v4.new_code_cell(code) ]
nbf.write(nb, 'test.ipynb')
# You can run this notebook from the command line (if you want):
jupyter nbconvert --execute --inplace test.ipynb

Refs:

--

--

Originally published at http://www.ds4n6.io.

Say you have been working a lot in your current notebook, you have lots of DataFrames and Series that get a long time to be computed, and you want to make sure you recover quickly if, for some reason, your Session (Kernel) dies.

The answer is “dill”.

Installation:

pip install dill

Save a Notebook session:

import dill dill.dump_session('notebook_env.db')

Restore a Notebook session:

import dill dill.load_session('notebook_env.db')

Caveats:

  • Fails when there are generators.
  • If you have pyodbc connection objects hanging around, you’ll need to close them and then set them all to None otherwise, you get a “TypeError: can’t pickle pyodbc.Connection objects” error.
  • The notebook state does not include graphs that were generated by your code, so you’ll need to rerun the cells to bring these back.

Refs:

https://stackoverflow.com/questions/34342155/how-to-pickle-or-store-jupyter-ipython-notebook-session-for-later

--

--

DS4N6

DS4N6

1 Follower

Community focused on bringing Data Science & Artificial Intelligence to the fingertips of the average Forensicator and promoting advances in the field.