import solara
@solara.component
def Page():
= solara.use_state("Happy")
name, set_name "Enter your name:", value=name, on_value=set_name)
solara.InputText("Greet", on_click=lambda: print(f"Hello, {name}!"))
solara.Button(f"Hello, {name}!")
solara.Text(
solara.display(Page())
import solara
import matplotlib.pyplot as plt
import numpy as np
@solara.component
def Page():
= solara.use_state(1.0)
a, set_a = solara.use_state(0.0)
b, set_b
= np.linspace(-10, 10, 100)
x = a * x + b
y
= plt.subplots()
fig, ax
ax.plot(x, y)
solara.FigureMatplotlib(fig)
"Slope (a)", value=a, min=-5, max=5, on_value=set_a)
solara.SliderFloat("Intercept (b)", value=b, min=-5, max=5, on_value=set_b)
solara.SliderFloat(
solara.display(Page())
@solara.component
def ThemeToggle():
= solara.use_state(False)
dark_mode, set_dark_mode
"Toggle Theme", on_click=lambda: set_dark_mode(not dark_mode))
solara.Button(if dark_mode:
"🌙 Dark Mode Activated", style={"color": "white", "background": "black", "padding": "10px"})
solara.Text(else:
"☀ Light Mode Activated", style={"color": "black", "background": "yellow", "padding": "10px"})
solara.Text(
ThemeToggle()
@solara.component
def TodoApp():
= solara.use_state([])
tasks, set_tasks = solara.use_state("")
new_task, set_new_task
def add_task():
if new_task.strip():
+ [new_task])
set_tasks(tasks "")
set_new_task(
"New Task", value=new_task, on_value=set_new_task)
solara.InputText("Add Task", on_click=add_task)
solara.Button(
for task in tasks:
f"✔ {task}")
solara.Text( TodoApp()
@solara.component
def Counter():
= solara.use_state(0)
count, set_count f"Count: {count}")
solara.Text("Increase", on_click=lambda: set_count(count + 1))
solara.Button(
Counter()
import solara
import random
= [
jokes "Why don't programmers like nature? It has too many bugs.",
"How do you comfort a JavaScript bug? You console it.",
"Why do Python programmers prefer dark mode? Because light attracts bugs!"
]
@solara.component
def Page():
= solara.use_state("Click the button to get a joke!")
joke, set_joke
def get_joke():
set_joke(random.choice(jokes))
="font-size: 18px; font-weight: bold;")
solara.Text(joke, style"Tell me a joke", on_click=get_joke)
solara.Button(
solara.display(Page())
import solara
import matplotlib.pyplot as plt
import numpy as np
@solara.component
def Page():
= ["Python", "JavaScript", "C++", "Java"]
options = {option: solara.use_state(0) for option in options}
votes
def vote(option):
1](votes[option][0] + 1)
votes[option][
= list(votes.keys())
labels = [votes[option][0] for option in options]
sizes
if sum(sizes) == 0:
= plt.subplots()
fig, ax 0.5, 0.5, "No votes yet!", fontsize=14, ha="center", va="center")
ax.text(# Hide axes
ax.set_xticks([])
ax.set_yticks([])else:
= plt.subplots()
fig, ax =labels, autopct="%1.1f%%")
ax.pie(sizes, labels
solara.FigureMatplotlib(fig)
for option in options:
f"Vote for {option}", on_click=lambda opt=option: vote(opt))
solara.Button(
solara.display(Page())