import gradio as gr import random import json import fastapi from fastapi import FastAPI # Create a FastAPI app app = FastAPI() # Sample country data with random progress percentages def generate_data(): return { "MX": {"name": "Mexico", "percent": random.randint(10, 90)}, "AR": {"name": "Argentina", "percent": random.randint(10, 90)}, "CO": {"name": "Colombia", "percent": random.randint(10, 90)}, "CL": {"name": "Chile", "percent": random.randint(10, 90)}, "PE": {"name": "Peru", "percent": random.randint(10, 90)}, "ES": {"name": "Spain", "percent": random.randint(10, 90)}, "BR": {"name": "Brazil", "percent": random.randint(10, 90)}, "VE": {"name": "Venezuela", "percent": random.randint(10, 90)}, "EC": {"name": "Ecuador", "percent": random.randint(10, 90)}, "BO": {"name": "Bolivia", "percent": random.randint(10, 90)}, "PY": {"name": "Paraguay", "percent": random.randint(10, 90)}, "UY": {"name": "Uruguay", "percent": random.randint(10, 90)}, "CR": {"name": "Costa Rica", "percent": random.randint(10, 90)}, "PA": {"name": "Panama", "percent": random.randint(10, 90)}, "DO": {"name": "Dominican Republic", "percent": random.randint(10, 90)}, "GT": {"name": "Guatemala", "percent": random.randint(10, 90)}, "HN": {"name": "Honduras", "percent": random.randint(10, 90)}, "SV": {"name": "El Salvador", "percent": random.randint(10, 90)}, "NI": {"name": "Nicaragua", "percent": random.randint(10, 90)}, "CU": {"name": "Cuba", "percent": random.randint(10, 90)} } # HTML template - avoiding f-strings with JavaScript template literals HTML_TEMPLATE = """ Latin America & Spain Map

Latin America & Spain Progress Map

""" # Route to serve the map visualization @app.get("/d3-map") async def serve_map(): # Generate random data country_data = generate_data() # Convert to JSON for JavaScript country_data_json = json.dumps(country_data) # Replace the placeholder with actual data html_content = HTML_TEMPLATE.replace("COUNTRY_DATA_PLACEHOLDER", country_data_json) return fastapi.responses.HTMLResponse(content=html_content) # Create a simple Gradio interface with an iframe def create_iframe(): # Add a random parameter to force reload random_param = random.randint(1, 10000) return ''.format(random_param) # Create the Gradio blocks with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="purple")) as demo: gr.Markdown("# Latin America & Spain Progress Map") iframe_output = gr.HTML(create_iframe()) # Refresh button to generate new random data def refresh(): return create_iframe() gr.Button("Generate New Data").click(fn=refresh, outputs=iframe_output) # Mount the Gradio app to the FastAPI app gr.mount_gradio_app(app, demo, path="/") # Start the server if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)