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()