googlecolab / colabtools

Python libraries for Google Colaboratory
Apache License 2.0
2.21k stars 724 forks source link

Add a cell from another cell. #4406

Open davies-w opened 8 months ago

davies-w commented 8 months ago

Is your feature request related to a problem? Please describe. I wish to programmatically add another cell into the notebook.

It's possible (unsupportedly) to read the entire notebook.

# Obtain the notebook JSON as a string
from google.colab import _message
import json
notebook_json_string = _message.blocking_request('get_ipynb', request='', timeout_sec=5)
print (json.dumps(notebook_json_string, sort_keys=True, indent=2))

There's also the ability to take this and add cells to it, and dump it as an ipynb file, and then, I assume (I haven't tried this), read it as a colab notebook. However, this isn't the same.

Describe the solution you'd like I'd like to be able to run a cell, and then have this code append a new cell (or insert at a specified position) to the colab notebook, and have it be saved, executed etc.

Describe alternatives you've considered See above. I do not see any way of doing this in Colab's version of Jupyter notebooks. There's some hint that it might work in Jupyter land. https://stackoverflow.com/questions/13614783/programmatically-add-cells-to-an-ipython-notebook-for-report-generation

Additional context Add any other context, screenshots or notebooks about the feature request here.

davies-w commented 8 months ago

Note this is not unrelated to https://github.com/googlecolab/colabtools/issues/42 - I'd actually tried %load and found it didn't work either, but it would have only been a half solution to what I want to do. I'm looking at using LLM code generation to read a description from a text box, and then insert the code required in the colab. It's not that dissimilar to the "AI" button, but would be more programmatically controllable.

colaboratory-team commented 8 months ago

Thanks for the feedback, we'll take that into consideration. In the meantime, two options that may work for you:

1) Forms are a handy way to supply parameters to code, and can be updated programmatically. See a demo notebook here https://colab.research.google.com/notebooks/forms.ipynb 2) You can insert code into a new scratch cell, which won't be saved to the notebook but can then be executed by the user:

from google.colab import _frontend
_frontend.create_scratch_cell('print("your code here")')
davies-w commented 8 months ago

Thanks Colab Team! I'll try the scratch cell approach - that might be enough for now, although I'd love to be able to alternate text and code programmatically!

davies-w commented 8 months ago

Hi,

Just some feedback here - the create_scratch_cell approach is pretty much what I needed, but it'd be really nice to have it have the option to make it a regular cell, and to have it append to the end of the current cells. Having the user review the code before pasting it in seems redundant from a security point of view, as you still need to manually run the cell (and the code is easier to read in that cell anyway). Or at least have a one time pop up that says "trust code to be pasted into an executable cell" for this colab.

One other "bug" that I've found is that if you approve the popup too soon, the cell hangs, and you can't execute the scratch cell. Maybe that's a feature, I don't know, but it feels like a bug.

cperry-goog commented 8 months ago

One other "bug" that I've found is that if you approve the popup too soon, the cell hangs, and you can't execute the scratch cell. Maybe that's a feature, I don't know, but it feels like a bug.

I can't get this to repro for myself - I use the above snippet, execute, immediately click authorize and the cell adds and executes no problem. Am I doing it wrong?

haesleinhuepf commented 4 months ago

the create_scratch_cell approach is pretty much what I needed, but it'd be really nice to have it have the option to make it a regular cell, and to have it append to the end of the current cells.

I agree with this. Would be great to have both options. The scratch-cells GUI seems unintuitive to me.