Read custom value from `index.html` in ZeroToBinderhub

Hi everyone,
is there a way on reading a custom value from index.html page in binderhub? I added a checkbox to html form and I need the value later in KubeSpawner.pre_spawn_hook. I can’t find a way how to propagate or at least read the value later.

Thanks for help!

Hi! Could you give us a bit more information about what you’d like to do? for example, which index.html page and html form are you referring to? What kind of custom values are you passing, and how do you want that value to be used?

Hi,
I have modified binderhub index.html with following

           <div class="row">                                                   
                <div class="row">                                               
                  <label>Would you like GPU?</label>                            
                </div>                                                          
                <div class="row">                                               
                  <input type="checkbox" id="gpu" name="gpu" value="yes">                                                                                                                  
                  <label for="gpu"> I need GPU</label><br>                      
                </div>                                                          
            </div> 

render as

The location of checkbox is not perfectly aligned but that’s not important now :slight_smile: I want to use the value later in the KubeSpawner.pre_spawn_hook and assign GPU if requested. The policy for assigning GPU was internally discussed in company so the checkbox being in form without any restrictions is not an issue.

In pre-spawn hook I want to perform something like

if checkbox.checked:
            spawner.extra_resource_guarantees = {"nvidia.com/gpu": 1 }    
            spawner.extra_resource_limits = {"nvidia.com/gpu": 1} 

I have deployed another zero2JH instance that has also form modified. However, I am able to query the values in pre spawn hook (e.g. image = spawner.user_options['container_image']) because I defined my own Spawner (class DockerImageChooser(KubeSpawner) ) and defined both form and function options_from_form:

          def options_from_form(self, formdata):                                
            """Parse the submitted form data and turn it into the correct       
               structures for self.user_options."""                             
                                                                                
            options = {}                                                        
            ...                                                                  
            dockerimage = formdata.get('dockerimage')[0]                                                             
            options['container_image'] = dockerimage   
            ...                         
            return options             

I can’t apply same approach for Binderhub because there is no options_from_form (or I haven’t found it).

I don’t think there’s an easy way to do that. The options form / user_options is created by the spawner, but BinderHub doesn’t provide direct access to it since it launches the singleuser itself by calling the JupyterHub API:

You’d therefore need to modify the BinderHub code to pass the parameters from your custom index page to where the API call is made.

Thanks. And do you know how approximately could I update the call? How are extra_args generated? Or how do I pass the value from index.html to these extra_args?

I don’t know the answer off hand, you’ll have to step through the code.