Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_simple_input(driver, display):
message_var = idom.Var(None)
@idom.element
async def Input(message=None):
message, set_message = idom.hooks.use_state(message)
message_var.set(message)
async def on_change(event):
if event["value"] == "this is a test":
set_message(event["value"])
if message is None:
return idom.html.input({"id": "input", "onChange": on_change})
else:
return idom.html.p({"id": "complete"}, ["Complete"])
display(Input)
def test_var_set():
v = idom.Var(None)
old_1 = v.set("new_1")
assert old_1 is None
old_2 = v.set("new_2")
assert old_2 == "new_1"
def test_same_origin_jupyter_display(driver, driver_wait, mount, server_url):
clicked = idom.Var(False)
@idom.element
async def SimpleButton(self):
@idom.event
async def on_click(event):
clicked.set(True)
return idom.html.button(
{"id": "simple-button", "onClick": on_click}, "click me same origin"
)
mount(SimpleButton)
driver.get(f"{server_url}/__test_jupyter_widget_client__")
client_button = driver.find_element_by_id("simple-button")
client_button.click()
def test_input_callback(driver, driver_wait, display):
inp_var = idom.Var(None)
inp = idom.widgets.Input(inp_var.set, "text", "initial-value", {"id": "inp"})
display(inp)
client_inp = driver.find_element_by_id("inp")
assert client_inp.get_attribute("value") == "initial-value"
client_inp.clear()
send_keys(client_inp, "new-value-1")
driver_wait.until(lambda dvr: inp_var.value == "new-value-1")
client_inp.clear()
send_keys(client_inp, "new-value-2")
driver_wait.until(lambda dvr: client_inp.get_attribute("value") == "new-value-2")
def test_input_ignore_empty(driver, driver_wait, display):
# ignore empty since that's an invalid float
inp_ingore_var = idom.Var("1")
inp_not_ignore_var = idom.Var("1")
@idom.element
async def InputWrapper():
return idom.html.div(
idom.widgets.Input(
inp_ingore_var.set,
"number",
inp_ingore_var.value,
{"id": "inp-ignore"},
ignore_empty=True,
),
idom.widgets.Input(
inp_not_ignore_var.set,
"number",
inp_not_ignore_var.value,
def linked_inputs(label, value, *types, **attributes):
var = idom.Var(value)
inputs = []
for tp in types:
inp = idom.Input(tp, value, attributes, cast=float)
@inp.events.on("change")
async def on_change(event, inp=inp):
for i in inputs:
i.update(inp.value)
var.set(inp.value)
inputs.append(inp)
fs = idom.html.fieldset(
{"class": "linked-inputs"},
[idom.html.legend({"style": {"font-size": "medium"}}, label)],
def __init__(self, grid_size, block_size):
self.snake = []
self.grid = Grid(grid_size, block_size)
self.new_direction = idom.Var(Directions.ArrowRight)
self.old_direction = idom.Var(Directions.ArrowRight)
self.food = idom.Var(None)
self.won = idom.Var(False)
self.lost = idom.Var(False)
def __init__(self, grid_size, block_size):
self.snake = []
self.grid = Grid(grid_size, block_size)
self.new_direction = idom.Var(Directions.ArrowRight)
self.old_direction = idom.Var(Directions.ArrowRight)
self.food = idom.Var(None)
self.won = idom.Var(False)
self.lost = idom.Var(False)