Skip to content

Instantly share code, notes, and snippets.

@michaelneale
Created January 7, 2021 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelneale/788e5e19d45f25063a4a3f404c651e6f to your computer and use it in GitHub Desktop.
Save michaelneale/788e5e19d45f25063a4a3f404c651e6f to your computer and use it in GitHub Desktop.
import streamlit as st
import pandas as pd
import altair as alt
import urllib
from streamlit.server.server import Server
from streamlit.report_thread import get_report_ctx
@st.cache
def get_UN_data():
AWS_BUCKET_URL = "https://streamlit-demo-data.s3-us-west-2.amazonaws.com"
df = pd.read_csv(AWS_BUCKET_URL + "/agri.csv.gz")
return df.set_index("Region")
try:
df = get_UN_data()
countries = st.multiselect(
"Choose countries", list(df.index), ["China", "United States of America"]
)
min_elts, max_elts = st.slider("How many %ss (select a range)?" % "boop", 0, 25, [10, 20])
if not countries:
st.error("Please select at least one country.")
else:
data = df.loc[countries]
data /= 1000000.0
st.write("### Gross Agricultural Production ($B)", data.sort_index())
data = data.T.reset_index()
data = pd.melt(data, id_vars=["index"]).rename(
columns={"index": "year", "value": "Gross Agricultural Product ($B)"}
)
chart = (
alt.Chart(data)
.mark_area(opacity=0.3)
.encode(
x="year:T",
y=alt.Y("Gross Agricultural Product ($B):Q", stack=None),
color="Region:N",
)
)
st.altair_chart(chart, use_container_width=True)
ctx = get_report_ctx()
print(ctx.query_string)
session_info = Server.get_current()._get_session_info(ctx.session_id)
print(session_info.ws.request.headers)
except urllib.error.URLError as e:
st.error(
"""
**This demo requires internet access.**
Connection error: %s
"""
% e.reason
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment