Skip to content

Instantly share code, notes, and snippets.

@parente
Last active November 30, 2022 04:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parente/4affb14e025cf805ebd21703f117bc5d to your computer and use it in GitHub Desktop.
Save parente/4affb14e025cf805ebd21703f117bc5d to your computer and use it in GitHub Desktop.
Jupyter Tidbit: Kernels for text files in JupyterLab

Summary

You can associate a Console panel and kernel with a text editor in JupyterLab, and use it to execute selected code, RStudio style.

  1. Create or open a text source file in JupyterLab.
  2. Right click in the text editor panel.
  3. Click Create Console for Editor.
  4. Select a kernel matched to the programming language you want to execute in the file.
  5. Notice the Console panel that appears.
  6. Highlight one or more lines back in the text editor you'd like to run.
  7. Press Shift-Enter to execute those lines.
  8. Notice the input and output that appears in the Console.

Example

Binder

Here's a screencast showing how to launch a kernel and console for a simple tryit.py file. Click the Binder button to try this example yourself.

Screencast of attaching a kernel and console to a tryit.py file in JupyterLab

Why is this useful?

You may want to develop a traditional Python (or other language) module alongside your notebooks. Attaching a kernel-plus-console to the source file allows you to iterate quickly on developing, executing, and evaluating your work without leaving the Jupyter environment or having to copy/paste code between a notebook / console and the module file.

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(10, 10))
df.head()
@hcchengithub
Copy link

This is called 'scratchpad' and I use it for everything I write with Jupyterlab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment