Jupyter Notebook


How to install Jupyter using miniconda?

conda install jupyter

How to view Jupyter notebook if Github page fails to render ipynb notebook?


How to parameterize, execute, and analyze Jupyter notebook?


How to format code in Jupyter notebook?

# install
jupyter nbextension install https://github.com/drillan/jupyter-black/archive/master.zip --user
# enable 
jupyter nbextension enable jupyter-black-master/jupyter-black

How to download all files in a path on Jupyter notebook server

  • Open a Jupyter notebook X on the server

    !tar cvfz allfiles.tar.gz *

    Reference

  • A file allfiles.tar.gz can be found in the directory where your X is localized. Click the file to download it.

  • Extract files from the .tar.gz in your local terminal
    tar -xvzf allfiles.tar.gz


How to know which Python and packages are used in jupyter notebook?

import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)

# Get locally imported modules from current notebook
import pkg_resources
import types
def get_imports():
    for name, val in globals().items():
        if isinstance(val, types.ModuleType):
            # Split ensures you get root package,
            # not just imported function
            name = val.__name__.split(".")[0]

        elif isinstance(val, type):
            name = val.__module__.split(".")[0]

        # Some packages are weird and have different
        # imported names vs. system/pip names. Unfortunately,
        # there is no systematic way to get pip names from
        # a package's imported name. You'll have to had
        # exceptions to this list manually!
        poorly_named_packages = {
            "PIL": "Pillow",
            "sklearn": "scikit-learn"
        }
        if name in poorly_named_packages.keys():
            name = poorly_named_packages[name]

        yield name
imports = list(set(get_imports()))

# The only way I found to get the version of the root package
# from only the name of the package is to cross-check the names
# of installed packages vs. imported packages
requirements = []
for m in pkg_resources.working_set:
    if m.project_name in imports and m.project_name!="pip":
        requirements.append((m.project_name, m.version))

for r in requirements:
    print("{}=={}".format(*r))

Reference