Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import time | |
| # π Base world population + growth rate | |
| BASE_POP = 8040000000 | |
| GROWTH_PER_SEC = 2.6 | |
| start_time = time.time() | |
| # π Live world population counter | |
| def live_world_population(): | |
| elapsed = time.time() - start_time | |
| current_pop = int(BASE_POP + elapsed * GROWTH_PER_SEC) | |
| return f"π World Population: {current_pop:,}" | |
| # π Get country population from API | |
| def get_country_population(country): | |
| try: | |
| url = f"https://restcountries.com/v3.1/name/{country}" | |
| response = requests.get(url, timeout=5) | |
| data = response.json() | |
| pop = data[0]["population"] | |
| return f"π {country} Population: {pop:,}" | |
| except Exception: | |
| return "β Country not found or API error" | |
| # π Country list | |
| countries = [ | |
| "India", "China", "United States", "Indonesia", | |
| "Pakistan", "Brazil", "Nigeria", "Bangladesh", "Russia" | |
| ] | |
| # π¨ Modern CSS | |
| css = """ | |
| body { | |
| background: linear-gradient(135deg, #141E30, #243B55); | |
| font-family: 'Poppins', sans-serif; | |
| color: white; | |
| } | |
| h1 { | |
| text-align: center; | |
| font-size: 42px; | |
| background: linear-gradient(90deg, #00c6ff, #0072ff); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| } | |
| .population-box { | |
| font-size: 26px; | |
| text-align: center; | |
| padding: 20px; | |
| border-radius: 15px; | |
| background: rgba(255,255,255,0.1); | |
| backdrop-filter: blur(10px); | |
| margin-top: 15px; | |
| } | |
| button { | |
| background: linear-gradient(90deg, #ff512f, #dd2476); | |
| border: none; | |
| border-radius: 10px; | |
| padding: 10px; | |
| color: white; | |
| font-weight: bold; | |
| transition: 0.3s; | |
| } | |
| button:hover { | |
| transform: scale(1.05); | |
| } | |
| """ | |
| # π§ UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π Live World Population Dashboard") | |
| world_output = gr.Textbox( | |
| label="Live World Population", | |
| elem_classes="population-box" | |
| ) | |
| gr.Markdown("## π Check Country Population") | |
| dropdown = gr.Dropdown( | |
| choices=countries, | |
| value="India", | |
| label="Select Country" | |
| ) | |
| country_output = gr.Textbox(elem_classes="population-box") | |
| btn = gr.Button("Get Population") | |
| # Button click event | |
| btn.click( | |
| fn=get_country_population, | |
| inputs=dropdown, | |
| outputs=country_output | |
| ) | |
| # π₯ Gradio 6 Timer for live updates | |
| timer = gr.Timer(1.0) | |
| timer.tick( | |
| fn=live_world_population, | |
| outputs=world_output | |
| ) | |
| # π Launch with CSS (Gradio 6 correct way) | |
| demo.launch(css=css) |