0

Suppose there is a Jupyter notebook file named template.ipynb in the current directory. What I want to do is to load its cells into the current notebook. It's different from the %run magic command, which executes the code in the notebook file and shows the output, or the %load magic command, which loads the code into the current cell. I want to load the cells into the current notebook, so that I can edit the code and execute it later. Is there any way to do this?

The reason I look for such capabilities is to allow user to load some preset template into their current notebook by just running a command, for example my_package.load_template('opencv') and then cells from the preset opencv template will be created in the current notebook.

Is there any magic command, for example, named %load_nb, which works like %load, but instead of loading code into the current cell, %load_nb should load another notebook from local file system or internet into the current notebook.

link89
  • 1,064
  • 10
  • 13
  • Are you looking to do this while it is running? You can do that by hand inside JupyterLab. You open each notebook side-by-side with the main work area split in half between the two notebooks, select the cells you want from the other notebook, and drag in the cells you want to the current notebook. If you don't know how to arrange the windows, you drag the tab with the name around and see how the main area adjusts. If you don't have JupyterLab installed and still want to use it to do such editing if your notebook, go [here](https://github.com/binder-examples/requirements) and click ... – Wayne Aug 28 '23 at 14:58
  • 'launch binder' to open an anonymous session where JupyterLab opens by default. You can drag-and-drop your local notebooks of your own machine into the file browser panel on the left side of the Jupyter page to upload them to the remote session. This is a temporary and anonymous session and so make sure you immediately download anything useful your make back to your own local machine. If you are trying to do this programmatically, you can do it to the notebook files using nbformat such as the way I describe [at the top here](https://stackoverflow.com/a/71244733/8508004). – Wayne Aug 28 '23 at 15:01
  • @Wayne What I want is I can put some templates in my package, so that users can load them by running, for example `my_package.use_template('opencv')` and then all the cells from the opencv will be loaded into notebook. – link89 Aug 28 '23 at 15:14
  • Did you see [programmatically add cells to an ipython notebook for report generation](https://stackoverflow.com/q/13614783/8508004)? I don't know if those work in current Jupyter. You don't describe what tech you are actually using in your post. I fear though you maybe are making an [XY problem](https://xyproblem.info/) for yourself. Your approach doesn't sound ideal. If you are trying to make an app that they don't see code, then do that. – Wayne Aug 28 '23 at 15:21
  • For 'templates': There's also [here](https://notebook.community/jepegit/cellpy/dev_utils/template_creation), [here](https://github.com/jupyterlab/jupyterlab/issues/1390), and [here](https://discourse.jupyter.org/t/lab-extension-open-a-notebook-dynamically-create-cell-at-top/4277). The latter is importantly from 2020. (I don't know about [here](https://johnhurleyphd.medium.com/create-a-template-for-your-jupyter-notebooks-80352d265cd4), but may have some ideas.) To get back to your approach, I don't see why you necessarily need them to see the code, and if so, just use `%load`? – Wayne Aug 28 '23 at 15:27
  • @Wayne The solution seems work to me. I am looking for a method to allow other users to load some notebook snippet into their notebook, so that they can run the cells or do some customization. It is not to hide code to users, instead I hope they can load the source code into their notebook and tailing the code to meet their needs. For example, I have some opencv code that other people have interest, but they don't want to just run the function I design for my need. Instead, they want to load my implementation to their notebook and tune for their own tasks. – link89 Aug 28 '23 at 15:32
  • I understand now, but I still fear any solution based on old tech will be brittle and possibly won't work in current JupyterLab or Jupyter Notebook V7. I wonder if [ipylab](https://github.com/jtpio/ipylab/tree/main) has something that helps here? Or maybe you are using current tech? It would be more informative for others in relation your last comment if you stated the versions involved? Playing devil's advocate given my concerns, I don't see how making a fancy coded way to add the blocks is less empowering than copy-paste from a site into the cell since the users need to edit anyway. – Wayne Aug 28 '23 at 16:44

0 Answers0