File size: 1,711 Bytes
0e559de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 26 15:04:49 2023
@author: jorge
"""

# StreamLit gapminder demo dashboard in 50 lines
# based on https://towardsdatascience.com/building-a-dashboard-in-under-5-minutes-with-streamlit-fd0c906ff886

import streamlit as st
import plotly.express as px

# Create three columns
# First and third columns are dummy to add a space to the sides
col1, col2, col3 = st.columns([5, 20, 5])

# Second column could host a streamlit image
# Commented image downloaded
with col2:
  st.title("Streamlit Demo")

# Get the user input
year_col, continent_col, log_x_col = st.columns([5, 5, 5])
with year_col:
  year_choice = st.slider(  # select year slider
    "What year would you like to examine?",
    min_value=1952,
    max_value=2007,
    step=5,
    value=2007,
  )
with continent_col:
  continent_choice = st.selectbox(  # select continent box
    "What continent would you like to look at?",
    ("All", "Asia", "Europe", "Africa", "Americas", "Oceania"),
  )
with log_x_col:  # log-scale check box
  log_x_choice = st.checkbox("Log X Axis?")

# Load the gapminder data frame
df = px.data.gapminder()

# Apply the year filter given by the user
filtered_df = df[(df.year == year_choice)]

# Apply the continent filter
if continent_choice != "All":
  filtered_df = filtered_df[filtered_df.continent == continent_choice]

# Create the figure in Plotly
fig = px.scatter(
  filtered_df,
  x="gdpPercap",
  y="lifeExp",
  size="pop",
  color="continent",
  hover_name="country",
  log_x=log_x_choice,
  size_max=60,
)
fig.update_layout(title="GDP per Capita vs. Life Expectancy")

# Associate Plotly chart to the Streamlit interface
st.plotly_chart(fig, use_container_width=True)