Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

How to open Julia files as a Jupyter Notebook locally in Visual Studio Code

Satoshi Terasaki edited this page Sep 7, 2024 · 1 revision

How to open .jl files as a Jupyter Notebook in Visual Studio Code

Description

In this page we will show how to open *.jl files (e.g., index.jl) stored in T4AJuliaTutorials repository as a Jupyter Notebook in VS Code. This instruction might be useful when one wants to update/modify/fix our files showing them in the form of Jupyter Notebook and get clear and meaningful diffs when doing version control.

The operations described below are demonstrated on Windows; however, these operations are the same across other operating systems such as macOS and Linux.

Why not store .ipynb files?

Since .ipynb files are in JSON format, they can be difficult for humans to read and compare difference between modified files and the original one. They happen to contain large image files, very long outputs and binary data which causes increasing the size of files. Generally speaking, pushing large files to Git repository is a bad practice. Git repository should contain source files, not results. If you commit large (exceeds 100MB) files to repository, GitHub may refuse your changes.

See GitHub Docs About size limits on GitHub to learn more. GitHub Docs says:

We recommend repositories remain small, ideally less than 1 GB, and less than 5 GB is strongly recommended. Smaller repositories are faster to clone and easier to work with and maintain.

Certainly, it is possible to establish a workflow or protocol to clear the output cells of a Jupyter Notebook before commiting/pushing their changes. In reality, however, researchers, scientists, and programming novices may not always be concerned with the protocol. Therefore, making protocols contains manual operation won't succeed in reality.

Instead of tracking .ipynb, we track source code as .jl files while this can be converted to .ipynb files via jupytext

About Jupytext

jupytext allows to save notebooks in the plain text, getting meaningful diffs when doing version control.

Viewing .jl files as a Jupyter Notebook in VS Code

In Visual Studio Code, also known as VS Code, you can install various extensions that enhance the workflow by allowing developers to edit files in a Jupyter Notebook format while continuing to store them as *.jl files.

Install extensions using code CLI

Open your terminal(Windows PowerShell if you are using Windows) and the following command:

PS> code --install-extension ms-toolsai.jupyter --install-extension julialang.language-julia --install-extension congyiwu.vscode-jupytext

You will get:

Installing extensions...
Installing extension 'congyiwu.vscode-jupytext'...
Installing extension 'julialang.language-julia'...
Installing extension 'ms-toolsai.jupyter'...
Extension 'congyiwu.vscode-jupytext' v0.1.2 was successfully installed.
Extension 'ms-toolsai.jupyter' v2024.6.0 was successfully installed.
Extension 'julialang.language-julia' v1.103.2 was successfully installed.
Extension 'ms-toolsai.jupyter-keymap' v1.1.2 was successfully installed.
Extension 'ms-toolsai.jupyter-renderers' v1.0.18 was successfully installed.
Extension 'ms-toolsai.vscode-jupyter-cell-tags' v0.1.9 was successfully installed.
Extension 'ms-toolsai.vscode-jupyter-slideshow' v0.1.6 was successfully installed.

Install extensions in VS Code

You can also install extension from VS Code GUI. Open VS Code by running the following commands:

PS> cd <path/to/T4AJuliaTutorials>
PS> code . 

Then type Ctrl + Shift + X (on macOS, Cmd + Shift + X) to open extensions view: In you want to install an extension for Julia, type Julia in the search box in extension view.

image

Then click Install button to install Julia's extension named julialang.language-julia.

To see what extensions are installed, type @installed in the search box. You would see the following output if you followed the previous instructions code --install-extension blah blah....

image

See Visual Studio Code documentation Browse for extensions to learn more.

Install Python (for Windows users only)

If you are using macOS or Linux, skip this section.

Since Jupytext is written in Python, you need to have a Python runtime to execute Jupytext's functionalities. This assumption, having Python runtime, is satisfied on Linux and macOS by default. Unlike most Unix systems and services, Windows does not include a system supported installation of Python. To make Python available on Windows, open Windows PowerShell and type

PS > python3

It will launch Microsoft Store and display store making us to install Python 3.12. Click Install button to install Python.

image

image

PS > python --version
Python 3.12.4

Open .jl files as a Jupyter Notebook

Open your terminal and open T4AJuliaTutorials directory:

PS> cd path/to/T4AJuliaTutorials
PS> code .

If you want to open index.jl as a Jupyter Notebook, right click and select Open as a Jupyter Notebook.

image

You will get the following results:

image

There is another approaching. On the top right in VS Code. You should see label icon Open as a Jupyter Notebook. Then click this.

image

How to run Jupyter Notebook with Julia kernel

To run Jupyter Notebook with a Julia kernel in VS Code, open the command palette in VS Code and type Notebook: Select Notebook Kernel

image

Then, choose the Julia release channel.

image

This action will display the Run All icon. Clicking this icon will execute all cells in the notebook.

image

As a result, you will see the output as expected:

image

More about Jupyter Notebook in VS Code

Refer the documentation: Working with Jupyter Notebooks in Visual Studio Code