How do I use snippets for Jupyter Notebook with VSCode?

Question:

I’ve been using VSCode for a while and at the moment I am trying to set up snippets to work. They seem to work well with simple Python (.py) files but not with Jupyter Notebook (.ipynb) files. Is there any way to make them work together?

The snippet is right here:

"Create a new figure":{
    "scope": "python",
    "prefix": "nf",
    "body": [
        "plt.figure(figsize=(9, 9))",
        "",
        "$1",
        "",
        "plt.show()"
    ]
}
Asked By: f3ss1

||

Answers:

I’m a developer on this extension. From the comments above it seems like the answer is already here. But just to put the official word in here. Currently our Notebook Editor is implemented as a custom webview with our own editor instances. Which means that while we have done our best to get lots of the look and feel right it’s not really a part of much of the main VS Code editing ecosystem. So things like snippits, keybindings, extensions like the VIM extension and Ctrl-F search are all currently not a part of it. Were looking both at implementing some of those things ourselves as well as working with VS Code team to bring our stuff more into the VS Code editor experience. If you want to track the work on this I’d recommend following the github item here:
https://github.com/microsoft/vscode-python/issues/7875

Update (11/18/21): New UI is out now so snippits should work in any notebook cell just as if it was a normal editor instance.

Answered By: Ian Huff

It is not possible to use user snippets in the Jupyter extension (yet), you can however use interactive python. When you write # %% in a normal .py file this is like opening a new Jupyter cell. You can convert a Jupyter notebook into a python file and it will use interactive python. There you will be able to use your snippets.

Answered By: TheFibonacciEffect

Try adding "markdown" in your snippet’s "scope":
"scope": "python,markdown"

Answered By: raftxo.mx