4

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()"
    ]
}
6
  • Did you try adding ipynb to the list of file extensions for python? code.visualstudio.com/docs/languages/identifiers Commented Dec 6, 2019 at 21:23
  • Yeap, that still seems not to be working. Here is the json part for it: "files.associations": { "*.ipynb": "python" } Commented Dec 7, 2019 at 12:45
  • I would raise an issue on Github. Since the python extension is loading inside Jupyter, it should just work. Ideally, it should work without any end-user configuration and because it is not, I think this should be a bug or a feature request. Commented Dec 7, 2019 at 22:46
  • Will do. Still, just a brief question: should all the custom extensions simply work with notebooks? Cause at the moment I have another one downloaded (to color the brackets) and it does not seem to work too. Commented Dec 8, 2019 at 9:04
  • I'm not sure if ipynb files register as simple text files. I think VSC does something fancy under the hood that doesn't adhere to the standard text editing notions. Saving a notebook is not as simple as writing the contents of the buffer to a file for example. I think most extensions may not work. Still, I think simple things like bracket highlighting should ideally work like they do in normal python scripts. Commented Dec 8, 2019 at 18:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I was wondering though: in File/Preferences/User Snippets there is a jupyter.json one which I supposed it was for snippets working in Jupyter Notebook files but it's not working for me: what is it used for?
Do I understand the GitHub issue resolution correctly? There will never be snippets in the Notebook Editor?
No that's not exactly the resolution. Our current implementation is based on the best we could currently do with VSCode, they only give extensions a webview (basically a blank browser page) to work with and we had to add our own editor instances into that, making supporting something like snippits very hard. However, vscode is now currently in the process of adding support for notebooks as a core part of VSCode. So when that comes along snippits will come with that. But we won't be adding them in the python extension as vscode support is already in progress.
@IanHuff what's the situation now? I was really curious if I can make premade notebooks one suggestion away for the students.
New UI is out now, so you should be able to use snippits in any notebook cell just as you would in an editor instance (given the language selected for that cell).
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.