Skip to content

Instantly share code, notes, and snippets.

@sonthonaxrk
Created January 31, 2021 16:49
Show Gist options
  • Save sonthonaxrk/cf805531e9c362ae16722f6e9439814a to your computer and use it in GitHub Desktop.
Save sonthonaxrk/cf805531e9c362ae16722f6e9439814a to your computer and use it in GitHub Desktop.
import asyncio
import ipywidgets as widgets
from IPython.display import display
def wait_for_click(button):
future = asyncio.Future()
def getvalue(button):
# make the new value available
future.set_result(True)
button.on_click(getvalue, remove=True)
button.on_click(getvalue)
return future
class InputGUI:
def __init__(self):
self.values = []
self._input = widgets.Text(
value='',
placeholder='Type something',
description='String:',
disabled=False
)
self._button = widgets.Button(description='Enter Input')
self._label = widgets.Label('Well done! Enter another input.')
self._root = widgets.Box([widgets.Label('Waiting...')])
def _ipython_display_(self):
"""
Display hook for Jupyter/QT notebooks
"""
display(self._root)
async def _input_loop(self):
self._root.children = [self._input, self._button]
await wait_for_click(self._button)
value = self._input.value
self._input.value = ''
# Show the message
self._root.children = [self._label]
return value
async def wait_for_entries(self, number):
list_of_inputs = []
for _ in range(number):
if list_of_inputs:
await asyncio.sleep(2)
list_of_inputs.append(await self._input_loop())
self._label.value = 'All done'
self._root.children = [self._label]
return list_of_inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment