0

i can press a button and get it to trigger events within the notebook, but when i export a html file, they no longer work.

import ipywidgets as widgets
from IPython.display import HTML
from ipywidgets import interactive
from ipywidgets import interact
from ipywidgets.embed import embed_minimal_html
from IPython.display import display
output = widgets.Output()

submitButton = widgets.Button(
  description='Click me',
  disabled=False,
  button_style='', # 'success', 'info', 'warning', 'danger' or ''
  tooltip='Click me',
  icon='check' # (FontAwesome names without the `fa-` prefix)
)

def buttonClick(btn):
  with output:
    print("Button clicked.")
  display(output)
  return True

submitButton.on_click(buttonClick)

def f(x):
  return x

display(submitButton)
submitButton.observe(buttonClick, names='value')
embed_minimal_html('export1.html', views=[submitButton, output], title='Widgets export')

I have tried using interact, interactive, and output; but can't get it to work. The html file should show the button and when clicked should say "Button clicked." The button shows but the text doesn't appear.

  • ^this isnt the whole program, but i have isolated the bug and reproduced the error with this code. if this code can be fixed, the program should be fixed – cameronmg-j Jul 15 '23 at 18:54
  • There's not a bug here. You aren't understanding how ipywidgets works. Running in the notebook, ipywidgets is using Python to run the Python portion of your code & so they work because you have an active Python kernel. Just converting a notebook to HTML by the standard route isn't going to result in the widgets working in the produced HTML. You've removed the active Python kernel & so the Python you have programmed in isn't going to work. Did you read the documentation ['Embedding Jupyter Widgets in Other Contexts than the Notebook'](https://ipywidgets.readthedocs.io/en/latest/embedding.html)? – Wayne Jul 15 '23 at 19:46
  • Fortunately, you are looking into this now and the landscape has radically changed in the last few months, none of which is reflected in that current documentation. Depending on what you need to accomplish, you can use a notebook in JupyterLite so it runs Python essentially in the user's browser so no need for active server, Voila + JupyterLite (which is called Voici ; see [here](https://github.com/voila-dashboards/voici-demo)), or now you can use Solara widgets. See [here for more about why I refer to that](https://twitter.com/kolibril13/status/1665085211900616704). – Wayne Jul 15 '23 at 19:50
  • By the way, this is mainly a duplication of several other posts already existing, such as [here](https://stackoverflow.com/q/63463702/8508004), [here](https://stackoverflow.com/q/64897511/8508004), [here](https://stackoverflow.com/q/43254861/8508004), and [here](https://stackoverflow.com/q/75700564/8508004), along with others that offer less guidance. **However, as my comments above point out things have changed a lot recently with the introduction of running Python in web assembly & Solara**, and so it would be nice to know if a direct straightforward solution to OP's needs exist now? – Wayne Jul 15 '23 at 19:57
  • @Wayne you legendddd! i looked at all the other posts you found and either couldn't get them to work or had already seen them. Solara works absolutely perfectly though, and if i want to it shouldn't be too hard to use solara widgets to change the gui too! if ipywidgets doesnt allow interactivity after html exporting anymore it feels dumb that they would have so many different ways of implementing it still shown in the documentation :/ tysm for ur help <3 – cameronmg-j Jul 17 '23 at 19:06
  • I think from your reaction Solara is going to cause that part of the ipywidgets documentation to need substantial revisiting. I'm glad Solara does what you need. Just to be clear, it is built on the foundation of ipywidgets, see [here](https://twitter.com/maartenbreddels/status/1671438376321335298) and [here](https://twitter.com/giswqs/status/1662567330990137348) and [here](https://twitter.com/maartenbreddels/status/1663079655425835009). So ipywidgets are in there in a big way. Plus, I should have mentioned anywidget, too see [here](https://twitter.com/trevmanz/status/1678913086319730690). – Wayne Jul 17 '23 at 19:47

1 Answers1

0

(see wayne's comment for more info) import solara and it should allow you to use ipywidgets and more in a separate web page.