Solara Code Examples

Author

Piyush , Himanshu , Mohit

Published

February 25, 2025

import solara

@solara.component
def Page():
    name, set_name = solara.use_state("Happy") 
    solara.InputText("Enter your name:", value=name, on_value=set_name) 
    solara.Button("Greet", on_click=lambda: print(f"Hello, {name}!"))
    solara.Text(f"Hello, {name}!")

solara.display(Page())

import solara
import matplotlib.pyplot as plt
import numpy as np

@solara.component
def Page():
    a, set_a = solara.use_state(1.0)
    b, set_b = solara.use_state(0.0)

    x = np.linspace(-10, 10, 100)
    y = a * x + b

    fig, ax = plt.subplots()
    ax.plot(x, y)
    solara.FigureMatplotlib(fig)

    solara.SliderFloat("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.display(Page())

@solara.component
def ThemeToggle():
    dark_mode, set_dark_mode = solara.use_state(False)  

    solara.Button("Toggle Theme", on_click=lambda: set_dark_mode(not dark_mode)) 
    if dark_mode:
        solara.Text("🌙 Dark Mode Activated", style={"color": "white", "background": "black", "padding": "10px"})
    else:
        solara.Text("☀ Light Mode Activated", style={"color": "black", "background": "yellow", "padding": "10px"})

ThemeToggle()

@solara.component
def TodoApp():
    tasks, set_tasks = solara.use_state([])  
    new_task, set_new_task = solara.use_state("")  

    def add_task():
        if new_task.strip():
            set_tasks(tasks + [new_task])  
            set_new_task("")  

    solara.InputText("New Task", value=new_task, on_value=set_new_task)  
    solara.Button("Add Task", on_click=add_task)  

    for task in tasks:
        solara.Text(f"✔ {task}") 
TodoApp()

@solara.component
def Counter():
    count, set_count = solara.use_state(0) 
    solara.Text(f"Count: {count}") 
    solara.Button("Increase", on_click=lambda: set_count(count + 1)) 

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():
    joke, set_joke = solara.use_state("Click the button to get a joke!") 

    def get_joke():
        set_joke(random.choice(jokes))  

    solara.Text(joke, style="font-size: 18px; font-weight: bold;")  
    solara.Button("Tell me a joke", on_click=get_joke)

solara.display(Page())  

import solara
import matplotlib.pyplot as plt
import numpy as np

@solara.component
def Page():
    options = ["Python", "JavaScript", "C++", "Java"]
    votes = {option: solara.use_state(0) for option in options} 

    def vote(option):
        votes[option][1](votes[option][0] + 1)  

    labels = list(votes.keys())
    sizes = [votes[option][0] for option in options]  

    
    if sum(sizes) == 0:
        fig, ax = plt.subplots()
        ax.text(0.5, 0.5, "No votes yet!", fontsize=14, ha="center", va="center")
        ax.set_xticks([])  # Hide axes
        ax.set_yticks([])
    else:
        fig, ax = plt.subplots()
        ax.pie(sizes, labels=labels, autopct="%1.1f%%")

    solara.FigureMatplotlib(fig)

    for option in options:
        solara.Button(f"Vote for {option}", on_click=lambda opt=option: vote(opt))  

solara.display(Page())