Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cell from another cell. #4406

Open
davies-w opened this issue Feb 28, 2024 · 6 comments
Open

Add a cell from another cell. #4406

davies-w opened this issue Feb 28, 2024 · 6 comments

Comments

@davies-w
Copy link

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
Copy link
Author

Note this is not unrelated to #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
Copy link
Contributor

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
Copy link
Author

davies-w commented Mar 1, 2024

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 davies-w closed this as completed Mar 6, 2024
@davies-w
Copy link
Author

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.

@davies-w davies-w reopened this Mar 12, 2024
@cperry-goog
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants