Visual studio code template for Jupyter notebook - visual-studio-code

Is there a way to have each Jupyter notebook file start with some preexisting template code? For example, when I create a .ipynb file, the first cell would have some import statements of my choosing.
I looked through the Jupyter extension settings in vscode, but I didn't see anything relevant.

Related

VS Code keeps making copies of my Jupyter Notebooks

I have a new installation of VS Code 1.62.3
When I open .ipynb files that were created in Jupyter Notebook and run the first piece of code, it keeps creating a second copy of the file with a hexidecimal name as a postfix.
Here is a screenshot of what it looks like:
This doesn't happen when I create the file in VS Code, only those created in Jupyter Lab.
Does anyone know why this is happening or how I can stop this?
Thank you

VS Code - How do I select Jupyter Kernel for a workspace?

I have some .ipynb files in a VS Code workspace (they're all in the same directory).
This is my workspace config at .vscode/settings.json:
{
"python.pythonPath": "/usr/local/anaconda3/bin/python"
}
I know how to set the default python interpreter for the workspace, but couldn't figure out a way to do the same for the jupyter kernel, and every time I open a notebook file, VS Code would prompt me to select a kernel for the notebook opened.
Is there a way to do this?

how to disable jupyter editor in vscode

like the title mentioned, how to disable jupyter editor in vscode?
every time,I want to new a file just for text or markdown,I do not like to appear a choice between two, but just default for the inner editor applied by vscode. and how can I configure this.
for right now,I never need to use jupyter notebook.
It seems to be appeared unexpected after vscode update or installed some python package.
This can be disabled by python.dataScience.useNotebookEditor if you are using the Python extension.
Source: https://github.com/microsoft/vscode/issues/103526
Update:
You also need the Jupyter extension along with Python extension for VS Code. The setting key has been renamed from python.dataScience.useNotebookEditor to jupyter.useNotebookEditor^update
With the July/August 2021 introduction of the new Native Notebook Support in VS Code, the answers involving your settings.json are now obsolete. Now the Notebook editor will be used to open .*ipynb files even if you've never installed the Python or Jupyter extensions.
However, you can open a *.ipynb file in the regular text editor by right-clicking on the file in the VS Code File Explorer, doing "Open With", and then selecting the "Text Editor".

VS Code not auto indenting in jupyter notebook / ipynb files

I must have accidentally disabled auto indent in VS code jupyter notebook. Does anyone know how to fix this? For example, indentation on the next line after creating a function.

VSCode: How to export a python file that was imported from a Jupyter Notebook back to Jupyter format?

Probably a silly question, but I couldn't find it. Visual Studio Code editor has a really nice way to work with Jupyter Notebooks. I can edit the cells directly in vscode and run them. Now it would be easy to work with version control.
But I couldn't find a way to convert it back to a Notebook! How do I generate a notebook back from the generated python file?
I understand that the notebook wouldn't have the output cells in it.
There is an option in the interactive Python window that has the notebook output:
This is really cool, now you can work in a Python file and have a really nice interface with your version configuration system (Git).
Yes, it was a silly question :-)
The Jupytext library supports the percent-based cell format used by VSCode-Python, as well as other text-based notebook interchange formats:
https://github.com/mwouts/jupytext
If you need more control over how conversion to ipynb is done (or you need to have cross-references) then you can give a try to Pandoctools. It can export VSCode *.py documents to any Pandoc output format or to Jupyter notebook.
For example you can create and register Jupyter kernel. For example is can be named "nn". That should be the same kernel that you selected in VSCode (there you select it by path but VSCode still uses installed kernels specs under the hood). Then add hat to the Python file, split document to cells, provide settings and set Markdown cells (commented metadata line would export to pdf instead of ipynb; I recommend to open ipynb in nteract native app):
"""
---
kernels-map:
py: nn
jupyter:
kernelspec:
display_name: nn
language: python
name: nn
pandoctools:
# out: "*.pdf"
out: "*.ipynb"
...
# Markdown section title 1
Some **static** Markdown text.
"""
# %% {echo=False}
import IPython.display as ds
import math
import sugartex as stex
# %% {markdown}
"""
# Markdown section title 2
The quick brown Fox jumps over the lazy dog.
"""
# %%
ds.Markdown(stex.pre(f'''
Some **dynamic** Markdown text with SugarTeX formula: ˎα^˱{math.pi:1.3f}˲ˎ.
It works because of the `Markdown` display option and `sugartex` Pandoc filter.
Acually `stex.pre` is redundant here but it is needed when the text is imported
or read from somewhere instead of being written in the same document.
'''))
Then convert the file via pandoctools: drag and drop file to pandoctools shortcut/executable or "open with" pandoctools executable.
Also see:
Two introduction articles are at the beginning of this README,
examples of input to output conversion that have cross-references!
how to use Pandoctools and it's CLI,
how to use Knitty that collects Jupyter outputs and change it's settings.
Use jupytext library:
Install:
pip install jupytext
Now open CMD or internal terminal to the folder with your .py file
Run this:
jupytext --set-formats py:percent,ipynb filename.ipynb