import ipywidgets as widgets
count = 0
button = widgets.Button(description="Clicked: 0")
def increment(btn):
global count
count += 1
button.description = f"Clicked: {count}"
button.on_click(increment)
page = button
import solara
# Declare reactive variables at the top level. Components using these variables
# will be re-executed when their values change.
sentence = "Solara makes our team more productive."
word_limit = 10
def on_word_limit_change(change):
print("on_word_limit_change")
global word_limit
word_limit = change["new"]
update_label()
def on_text_change(change):
print("on_text_change")
global sentence
sentence = change["new"]
update_label()
def update_label():
word_count = len(sentence.split())
if word_count >= word_limit:
label.value = f"With {word_count} words, you passed the word limit of {word_limit}."
label.style.background = "red"
elif word_count >= int(0.8 * word_limit):
label.value = f"With {word_count} words, you are close to the word limit of {word_limit}."
label.style.background = "orange"
else:
label.value = "Great short writing!"
label.style.background = "lightgreen"
slider = widgets.IntSlider(description="Word limit", value=word_limit, min=2, max=20)
slider.observe(on_word_limit_change, "value")
text_input = widgets.Textarea(description="Your sentence", value=sentence)
text_input.observe(on_text_change, "value")
label = widgets.Label(value="")
update_label()
# The following line is required only when running the code in a Jupyter notebook:
# Page()
page = widgets.VBox([slider, text_input, label])