Skip to content

Instantly share code, notes, and snippets.

@john-adeojo
Created March 21, 2023 17:32
Show Gist options
  • Save john-adeojo/abcc8bb387e829da941d6d1bc5b6bf41 to your computer and use it in GitHub Desktop.
Save john-adeojo/abcc8bb387e829da941d6d1bc5b6bf41 to your computer and use it in GitHub Desktop.
import streamlit as st
import pandas as pd
import folium
from folium.plugins import MarkerCluster
from streamlit_folium import folium_static
tweets_dash_final =pd.read_csv(path_to_csv)
# Define a function to assign emojis based on emotion
def get_emoji(sentiment):
if sentiment == "positive":
return "😁"
elif sentiment == "neutral":
return "😐"
elif sentiment == "negative":
return "πŸ˜‘"
else:
return "❓"
# Apply the get_emoji function to the 'emotion' column
tweets_dash_final['emoji'] = tweets_dash_final['sentiment'].apply(get_emoji)
def create_map(tweets_dash_final):
# Create a base map centered on the UK
map = folium.Map(location=[51.5074, -0.1278], zoom_start=6)
# Assuming you have a list of topics
topics = ['Customer Service', 'Philately', 'Politics', 'Royal Reply', 'Royal Mail Jobs', 'Financial News']
topic_layer_groups = {}
# Create a LayerGroup for each topic and add it to the map
for topic in topics:
topic_layer_groups[topic] = folium.FeatureGroup(name=topic)
map.add_child(topic_layer_groups[topic])
# Loop through each row in the DataFrame and add a marker with an emoji to the appropriate layer
for index, row in tweets_dash_final.iterrows():
lat, lon = row['latitude'], row['longitude']
emoji = row['emoji']
topic = row['topic']
cleaned_text = row['cleaned_text']
marker = folium.Marker(
location=[lat, lon],
icon=folium.DivIcon(html=f"""<div style="font-size:24px;">{emoji}</div>"""),
tooltip=cleaned_text
)
topic_layer_groups[topic].add_child(marker)
# Add layer control to switch between layers
map.add_child(folium.LayerControl())
return map
# Streamlit app
st.title("Topic & Sentiment Map of #royalmail Tweets")
st.sidebar.title("Options")
map = create_map(tweets_dash_final)
folium_static(map)
st.text('This is for prototyping purposes only')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment