Py.Cafe

fomightez/

super-basic-guessing-game-in-ipywidgets-with-markdown-converted2Solara

super basic guesing game with markdown converted

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
# I'm not happy with this. I don't see how to make it
# only react when I release mouse?!?! Maybe slider
# isn't what I want here for solara because cannot disable continuous_update, it seems? See https://github.com/widgetti/solara/issues/262
import solara
import random

initial_value = 0

# Generate a secret random number
secret = random.randint(1, 10)

# Define a reactive variable to store the user's guess
guess = solara.reactive(initial_value)

# Define a reactive variable to track if the user guessed correctly
guessed_correctly = solara.reactive(False)

# Define a reactive function to check the guess
def check_guess(guess, secret):
    if guess.value == initial_value:
        return "The secret number is a number between 1 and 10. Drag the slider to guess."
    elif guess.value == secret:
        return f"Your current guess:{guess.value}     **You got it!**"
    elif guess.value < secret:
        return f"Your current guess:{guess.value} &nbsp; &nbsp;  **Too small**"
    else:
        return f"Your current guess:{guess.value} &nbsp; &nbsp;  **Too big**"

@solara.component
def Page():
    # Create a SliderInt for the user's guess
    slider = solara.SliderInt("Slide to Your Guess:", value=guess, max=10)

    # Display the feedback output using Markdown
    solara.Markdown(check_guess(guess, secret))

    # Define a function to reset the game
    def reset_game():
        guess.set(0)
        guessed_correctly.set(False)

    # Add a button to reset the game
    with solara.Row():
        solara.Button("Reset", on_click=reset_game)

# Assign the Page component to the page variable
page = Page()