# 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} **Too small**"
else:
return f"Your current guess:{guess.value} **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()