awacke1's picture
Create app.py
81b6f31
raw
history blame
2.76 kB
import streamlit as st
# Function to parse the data for a specific state using its two-letter code
def parse_state_data(data, state_code):
state_data = {}
current_category = None
for line in data:
line = line.strip()
if line.startswith(state_code + ":"):
state_data["Name"] = line[len(state_code) + 1:]
elif line.startswith("# Large Companies"):
current_category = "Large Companies"
state_data[current_category] = []
elif line.startswith("# Cities"):
current_category = "Cities"
state_data[current_category] = []
elif line.startswith("# Hospitals"):
current_category = "Hospitals"
state_data[current_category] = []
elif line.startswith("#"):
current_category = None
elif current_category:
state_data[current_category].append(line)
return state_data
# Function to create a search URL for Wikipedia:
def create_search_url_wikipedia(query):
base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
return base_url + query.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
# Function to create a search URL for YouTube:
def create_search_url_youtube(query):
base_url = "https://www.youtube.com/results?search_query="
return base_url + query.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
# Read and parse the data from the text file
with open("states_data.txt", "r") as file:
data = file.readlines()
# Streamlit page configuration
st.set_page_config(page_title="State Data", layout="wide")
# Main title
st.title("Top Five Lists for Different States 🏙️")
# Select a state using a two-letter code
selected_state = st.selectbox("Select a State:", ["MA: Massachusetts", "CA: California", "WA: Washington"])
# Parse the data for the selected state
state_code = selected_state.split(":")[0]
state_data = parse_state_data(data, state_code)
# Display the state name
st.header(f"{state_data['Name']} 🏆")
# Display the top five lists
for category, nominees in state_data.items():
if category != "Name":
st.subheader(f"{category}")
with st.expander(f"View {category} Nominees"):
for nominee in nominees[:5]: # Show only the top five
col1, col2, col3 = st.columns([4, 1, 1])
with col1:
st.markdown(f"* {nominee}")
with col2:
st.markdown(f"[Wikipedia]({create_search_url_wikipedia(nominee)})")
with col3:
st.markdown(f"[YouTube]({create_search_url_youtube(nominee)})")
# Footer
st.caption("Source: Wikipedia and YouTube")