skip to Main Content

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 VS Code, but I didn’t see anything relevant.

2

Answers


  1. This extension (File Templates) should help.
    Simply create a new .ipynb file and enter whatever imports and code you want to replicate, then use the command palette to select "New template from file". Then, whenever you need to create a new .ipynb file, go to the command palette, select, "New file from template" and select the appropriate template and you should be good to go.

    This solution is not quite as simple, but it should take a similar amount of effort and is about as close as you can get.

    Login or Signup to reply.
  2. You can just use Jupyter Notebooks’ builtin mechanism for this. https://ipython.readthedocs.io/en/stable/interactive/tutorial.html#startup-files. Create .py or .ipy files in your profile_default/startup/ directory (typically located within ~/.ipython/), and they’ll be run as soon as the IPython shell is created, and before your other code/scripts, in the order of their names. See also the Jupyter docs on Python configuration files.

    You will need to restart the kernel for the changes in those files to take effect. Use the restart button to restart the kernel, or the Notebook: Restart Kernel command in the command palette.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search