File size: 1,046 Bytes
b152d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random

def generate_random_number():
    """
    Generates a random number between 1 and 100.
    Takes a dummy argument to comply with Gradio's expectations when triggered by a button.
    """
    return random.randint(1, 100)

# The 'Button' component triggers the action. In Gradio 4.5.0, components are more streamlined,
# and you might not need to specify the input component type as explicitly.
# Here, the button is directly linked to the action function.
# Note: Unlike previous versions, if your function expects an argument from an input component,
# you must ensure the function can accept it, even if the input is not used.
# Hence, the dummy parameter in 'generate_random_number' function.

with gr.Blocks() as demo:
    gr.Markdown("# Random Number Generator")
    gr.Markdown("Click the button to generate a new random number.")
    button = gr.Button("Refresh Number")
    output = gr.Number(label="Random Number")

    button.click(fn=generate_random_number, inputs=[], outputs=output)

demo.launch()