Last active
September 29, 2021 09:15
-
-
Save maximlt/a0cbfc86cd96f72f36b14140f13b1237 to your computer and use it in GitHub Desktop.
Create a notebook including a title and some imports and run it in a conda environment with Jupyter Lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from datetime import date | |
import pathlib | |
import subprocess | |
import sys | |
def create_nb(nb_dir: str, extra_title: str = None) -> pathlib.Path: | |
today = date.today() | |
title = today.strftime("%B %d, %Y") | |
fn = today.strftime("%Y-%m-%d") | |
if extra_title: | |
title = title + ' - ' + extra_title | |
fn = fn + '_' + extra_title.replace(' ', '-').lower() | |
nb_template = r""" | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "2c699017-290e-4066-b15f-7fd34dd133e2", | |
"metadata": {}, | |
"source": [ | |
"# $title$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bd4f7d0b-82ed-4731-a255-023de1b8e6d8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import bokeh as bk\n", | |
"import holoviews as hv\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import panel as pn\n", | |
"import param\n", | |
"import xarray as xr\n", | |
"\n", | |
"for m in [bk, param, hv, pn, np, pd, xr]:\n", | |
" print(f'{m.__name__} {m.__version__}')\n", | |
"\n", | |
"pn.extension()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} | |
""".lstrip() | |
nb_content = nb_template.replace('$title$', title) | |
nb_dir = pathlib.Path(nb_dir).expanduser() | |
fp = nb_dir / f'{fn}.ipynb' | |
i = 1 | |
while fp.exists(): | |
fp = nb_dir / f'{fn}-{i}.ipynb' | |
i += 1 | |
with open(fp, 'w') as f: | |
f.write(nb_content) | |
print(f'Notebook created: {fp}') | |
return fp | |
def run_nb(nb_fp: str, conda_env: str, nb_dir: str) -> int: | |
print(f'Trying to run notebook {nb_fp} in folder {nb_dir} in conda env {conda_env}') | |
try: | |
result = subprocess.check_output(['conda', 'run','-n', conda_env, 'jupyter', 'lab', '--notebook-dir', nb_dir, nb_fp]) | |
except subprocess.CalledProcessError as err: | |
return sys.exit(err.returncode) | |
return 0 | |
def cli(): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--env', default='holoviz37') | |
parser.add_argument('--dir', default='~/work/notebooks') | |
parser.add_argument('-t', '--extratitle') | |
args = parser.parse_args() | |
nb_fp = create_nb(args.dir, args.extratitle) | |
nb_dir = str(pathlib.Path(args.dir).expanduser()) | |
nb_fp = str(nb_fp) | |
code = run_nb(nb_fp, args.env, nb_dir) | |
sys.exit(code) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment