Py.Cafe

shiny-unofficial/

solara-word-limit-utilities

Solara Word Limit Utilities

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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])