Jupyter Tidbit: Kernels for text files in JupyterLab
August 24, 2018
This post originates from a gist that supports comments, forks, and execution in binder.
You can associate a Console panel and kernel with a text editor in JupyterLab, and use it to execute selected code, RStudio style.
- Create or open a text source file in JupyterLab.
- Right click in the text editor panel.
- Click Create Console for Editor.
- Select a kernel matched to the programming language you want to execute in the file.
- Notice the Console panel that appears.
- Highlight one or more lines back in the text editor you'd like to run.
- Press Shift-Enter to execute those lines.
- Notice the input and output that appears in the Console.
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.
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.