简体   繁体   中英

VScode: Automating conversion of python file to Jupyter notebook with a task

In VScode I can write a python file with markdown and python cells and then convert it to a notebook via the command palette. Everything works well, but I would like to automate this with a task.

I know I could just define a shortcut for the conversion but then I would still need to manually save the notebook with the file explorer. Can I automate this with a task? If so how do I access the function for the conversion? Is this some internal function of VScode or can I access this function via the command line?

I tried a few things with the jupyter command in the command line but didn't have any luck. It seems like there is no command to convert a python file to a Jupyter notebook. Also I could not find a comprehensive documentation for the jupyter command.

Another question in regards to Jupyter notebooks in VScode: Is there a way how I can hide a cell from showing up? I know it would be possible if I edit the metadata of the notebook, but I hope there is a better way.

In case anyone else is still wondering about this, I'm now using jupytext for converting python files to jupyter notebooks.

I wrote two very simple bash scripts to automate the conversion and viewing of notebooks.

conversion:

#!/usr/bin/env bash

# retrieve file names and folder from arguments
full_fname=$1
fbase_name_no_ext=$2
dir_name=$3
workspace_folder=$4

full_path_no_ext="$dir_name/$fbase_name_no_ext"

notebook_save_path=$(cd ${dir_name}/.. && pwd)

# activate venv (venv should be in vscode workspace root)
source "${workspace_folder}/my_env/bin/activate"

echo "saving to: $notebook_save_path"

# convert to jupyter notebook
jupytext --to notebook ${full_fname}

# run all cells and save output to notebook file
jupyter nbconvert --to notebook --execute "${full_path_no_ext}.ipynb" --output "${notebook_save_path}/${fbase_name_no_ext}"

# cleanup intermediate notebook (contains only cells no output)
rm "${full_path_no_ext}.ipynb

viewing a notebook:

#!/usr/bin/env bash

# retrieve file names and folder from arguments
fbase_name_no_ext=$1
dir_name=$2
workspace_folder=$3

source "${workspace_folder}/my_env/bin/activate"

# notebooks are stored in the parent directory of the source file
notebook_folder=$(cd ${dir_name}/.. && pwd)

# view notebook in a browser window
jupyter notebook "${notebook_folder}/${fbase_name_no_ext}.ipynb"

Here is my vscode tasks file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "convert to NB",
            "type": "shell",
            "command": "${workspaceFolder}/convert",
            "args": [
                "${file}",
                "${fileBasenameNoExtension}",
                "${fileDirname}",
                "${workspaceFolder}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            }
        },
        {
            "label": "view NB",
            "type": "shell",
            "command": "${workspaceFolder}/viewNB",
            "args": [
                "${fileBasenameNoExtension}",
                "${fileDirname}",
                "${workspaceFolder}"
            ]

        }
    ]

Since I'm no bash expert, there might be multiple things that could be done in a better way. Critique is always welcome!
Hope this is helpful to someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM