Achieving the equivalent of Google CoLab @param markup in Jupyter without CoLab
Asked Answered
S

2

14

Google Colab has some unique embedded markdown features which are not present in Jupyter markdown.

For example, this produces a slider:

#@title SEIR Model with Social Distancing { run: "auto" }
#@markdown 
#@markdown Reproduction number
R0 = 2.4 #@param {type:"slider", min:0.9, max:5, step:0.1}

Attempts to run Colab locally seem to be negative: CoLab notebooks must pass through the Google CoLab website to operate.

What is the best way to produce the equivalent of the @param markup from CoLab in an open-source way that works on Jupyter in a locally run notebook without going through an external website?

Schuster answered 28/6, 2020 at 20:24 Comment(2)
I'd love to know that too! Do you have an update since?Raynold
Sorry no. Since then I think plotly may offer better GUI prospects or other packages than using markdown.Schuster
D
4

I've struggled with the same problem and I solved it by using ipywidgets Jupyter Widgets documentation It is not as elegant, but works.

@interact_manual(R0 = (0.9,5.1,0.1))
def my_function(R0 = 2.4):
   print("SEIR Model with Social Distancing")
   print("Reproduction number")
   print(R0) # do something - enter your code here
   

What happens is, you will get a slider with your min/max values and a step. I am using _manual - you can set multiple parameters and after you've set them the way you want, you can click on the "Run Interact" button, which calls your function (e.g. my_function).

Image: Widget in VSCode

Works with VS-Code but not PyCharm

Decadent answered 19/4, 2022 at 13:29 Comment(3)
I'm curious why Google didn't add this to Jupyter rather than making it a carve-out. Making CoLab incompatible with Jupyter makes me a lot less likely to want to use CoLab, and I am frustrated by projects that split their content messily between CoLab, GitHub and other services.Schuster
Well, the thing is, they probably want to have something extra, so you would choose CoLab instead of local Jupyter. Especially if you use it alot, get used to it and afterwards pay for it.Decadent
I find that something extra to be off-putting and also it leads people down a path where they don't end up with something you can download from GitHub onto a single machine and easily reproduce results with all pieces in one place. For example a speech recognition project with pieces spread across GitHub, CoLab and HuggingFace so that the net is unusable for someone who didn't write the code to begin with, but to those who wrote the code and have all those accesses, it seems simple and self-documenting.Schuster
L
1

So I'm not sure if they fully publish the code for this, but Google collab has several open-source elements in repos on github:

https://github.com/orgs/googlecolab/repositories

I suspect that what they do is map the Ipywidgets to some custom javascript (or typescript) widgets and then have a magic or some such that interprets a notebook on open to automatically run ipywidgets or something similar. They have several repos that talk about widgets but I'm not sure if they have some custom javascript somewhere that gets loaded in a config file.

I wouldn't suspect it would take someone too much effort to map this and create a jupyter notebook and/or jupyter lab extension that automatically goes through the code on load and generates the form, but it's currently beyond my skill/interest to reverse engineer.

Laufer answered 16/10, 2022 at 2:56 Comment(1)
So, another method to do something similar might be with gradio and their %%blocks magic that re-run a cell whenever you change it. gradio is really meant for user-input/ML model app presentation from a notebook, but it's about as close as I've seen. You'll have to manually specify the components, but it shouldn't be too hard to write a module on top of gradio that would parse the python in a cell and add a form based on the comments.Laufer

© 2022 - 2024 — McMap. All rights reserved.