Spaces:
Sleeping
Sleeping
import gradio as gr | |
import threading | |
# Hello World Gradio app as a string | |
hello_world_code = """ | |
import gradio as gr | |
def greet(name): | |
return f"Hello, {name}!" | |
iface = gr.Interface(fn=greet, inputs="text", outputs="text") | |
iface.launch(server_name="0.0.0.0", server_port=7860) | |
""" | |
# Function to execute the Gradio app dynamically | |
def run_gradio_app(): | |
try: | |
exec(hello_world_code, {"gr": gr}) | |
except Exception as e: | |
print(f"Error executing generated code: {e}") | |
# Function triggered on button click | |
def launch_app(): | |
threading.Thread(target=run_gradio_app, daemon=True).start() | |
return "Hello World Gradio app launched! Refresh to see it." | |
# Gradio UI | |
with gr.Blocks() as ui: | |
gr.Markdown("### Basic Dynamic Gradio Loader") | |
launch_button = gr.Button("Launch Hello World App") | |
output_text = gr.Textbox(label="Status") | |
launch_button.click(launch_app, outputs=output_text) | |
# Start the initial UI | |
if __name__ == "__main__": | |
ui.launch(server_name="0.0.0.0", server_port=7860) |