Navigate back to the homepage

Automating Jupyter Lab setup

Juho Salli
December 11th, 2020 · 1 min read

Hero image credits to Maxim Hopman on Unsplash.

Motivation

More often than not, I find myself setting up a new python virtual environment with the same packages required for my basic data analysis workflow. It’s always the same steps:

  1. Create a virtualenv
  2. Source the new venv
  3. pip install the same packages
  4. Create a new notebook for Jupyter
  5. Import the same few packages
  6. Finally start the work

Sometimes I do these steps multiple times per day and over time that accumulates to quite a lot time just to do non-productive repetative work.

So I automated the first 5 of the steps listed above.

Solution

TL;DR: You can find the code here.

I made a git repo with all the necessary files for this process and wrote a bash-script that does all the installing.

1❯ tree -L 1
2.
3├── LICENSE
4├── README.md
5├── install.sh
6├── requirements.txt
7├── starter.ipynb
8└── venv
1#! /bin/bash
2
3# First create a python3 virtualenv
4python3 -m venv venv
5
6# Then source the newly created env
7source venv/bin/activate
8
9# Next, install the requirements
10pip install -r requirements.txt
11
12echo 'Done!'
13echo 'Run `source venv/bin/activate`'
14echo 'And after that `jupyter lab`'

Now I could just clone the repo, run the script and start the actual work.

However, the process has now only few steps less than the original one had.

To make this better (ie. faster), I wrote a script that I can just curl and run.

1#! /bin/bash
2
3# clone from github to current directory
4# TODO make the dir as an argument to be passed in
5git clone git@github.com:juhosa/jupyter-lab-template.git .
6
7# run the installer
8./install.sh
9
10# remove the .git directory
11rm -rf .git/

Now when I run

1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/juhosa/jupyter-lab-template/main/clone_and_install.sh)"

I’ll get a basic setup in no time compared to the original. Sweet! 🎉

Thinking about this, I’ll propably make an alias for the setup script call. That way I could just call something like

1jupytersetup

and don’t have to worry about finding the clone-and-install script.

-Juho

More articles from Juho Salli

Computing relative dates in pandas

Find the difference of dates in days.

May 1st, 2020 · 1 min read

From bug to fix to production

Sometimes finding the fix for a bug is fast.

August 30th, 2019 · 1 min read
© 2019–2020 Juho Salli
Link to $https://twitter.com/juhosaLink to $https://github.com/juhosaLink to $https://instagram.com/juhosalliLink to $https://www.linkedin.com/in/juhosalli/