question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create new button from on_click() method OR create 2nd button during 1st button press event

See original GitHub issue

I am trying to create a button run time. Example, On click ‘Load’ button, create ‘Test’ button. How do I achieve this feature in widgets? ‘’‘Code here’‘’

from ipywidgets import Button
def mycallbackk(b):   
    btn1=Button(description='test')
    display(btn1)
    pass
    

btn = Button(description='Load')
btn.on_click(mycallbackk)
display(btn)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jasongroutcommented, Nov 14, 2019

Thanks for following up with a solution!

As a tip, enclose code in three backticks to have it formatted nicely (I edited your comment so you can see how to do it).

0reactions
shek2994commented, Nov 14, 2019

Alternatively, you could create a hbox at the global level, and in the callback add the button to the hbox, which would then automatically display it.

Thanks, @jasongrout , your 2nd method works.

Here is the code:


from ipywidgets import (widgets, Button, Text, IntText, FloatText, Dropdown, Output,
                       VBox, HBox)
from IPython.display import display
import json

from ipyfilechooser import FileChooser

''' Create and display a FileChooser widget'''
fc = FileChooser('./')
display(fc)
out = widgets.Output()
display(out)


t = None
vb = VBox()
wi_list = list()
    
def mycallback(b):
    
    for wi_obj in wi_list:
        with out:
            print('found =',wi_obj)
        wi_obj.close()  

    wi_list.clear() #NOTE:  clear list containing widget obj for previous json file. Also destroy wi obj before selecting another json file
        
    with out:
        print(b, fc.selected)
    json_config = None
    with open(fc.selected, 'r') as fp:
        json_config = json.load(fp)
    for k, v in json_config.items():
        t = None

        if type(v) == type('str') :
            t = Text(description=k, style = {'description_width': 'initial'})
            t.value = v; wi_list.append(t)        
        elif type(v) == type(0.23) :
            t = FloatText(description=k, style = {'description_width': 'initial'})
            t.value = v; wi_list.append(t)
        elif type(v) == type(1) :
            t = IntText(description=k, style = {'description_width': 'initial'})
            t.value = v; wi_list.append(t)
        elif type(v) == type(True) :
            t = Dropdown(description=k, style = {'description_width': 'initial'})
            t.options = [True, False]; wi_list.append(t)
        else :
            t = Dropdown(description=k, style = {'description_width': 'initial'})
            t.options = [None]; wi_list.append(t)
    
    
    vb.children = list(wi_list)
    
    pass
    

btn = Button(description='Load')
btn.on_click(mycallback)
display(btn,vb)
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Button onclick – JavaScript Click Event Tutorial
In this tutorial, we are going to explore the two different ways of executing click events in JavaScript using two different methods. First...
Read more >
Call two functions from same onclick [duplicate] - Stack Overflow
You can create a single function that calls both of those, and then use it in the event.
Read more >
onclick Event - W3Schools
onmousedown, A mouse button is pressed over an element ... In JavaScript, using the addEventListener() method: object. ... Use onclick to create a...
Read more >
How to Use the JavaScript onClick Event: A Step-by-Step Guide
First, you must select the element to which you want to add the onClick event. For our button example, we would use the...
Read more >
Handling Click events in Button | Android - GeeksforGeeks
When the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found