alperugurcan's picture
Update app.py
6f84854 verified
Raw
History Blame Contribute Delete
3.31 kB
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pytrends.request import TrendReq
# Set up pytrends
trends = TrendReq()
# Function to get data
def get_data(keywords, geo, timeframe):
trends.build_payload(kw_list=keywords, geo=geo, timeframe=timeframe)
data = trends.interest_over_time()
data = data.drop(columns=['isPartial'])
return data.reset_index()
# Streamlit app
st.title("Google Trends Analysis")
# User inputs
keywords = st.text_input("Enter keywords (comma-separated)", "yapay zeka, makine öğrenmesi, derin öğrenme").split(',')
geo = "TR" # Automatically set to TR
timeframe = st.text_input("Enter timeframe (e.g., today 5-y, 2022-01-01 2023-01-01)", "today 5-y")
# Display the selected geo
st.write(f"Selected location: Turkey (TR)")
# Get data
if st.button("Fetch Data"):
data = get_data(keywords, geo, timeframe)
st.session_state['data'] = data
st.success("Data fetched successfully!")
# Visualization options
if 'data' in st.session_state:
viz_options = [
"Line Plot",
"Heatmap",
"Bar Plot",
"Area Plot",
"Violin Plot",
"Box Plot",
"Scatter Plot",
"Pair Plot"
]
selected_viz = st.selectbox("Select Visualization", viz_options)
if selected_viz == "Line Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(data=st.session_state['data'].melt(id_vars=['date'], var_name='term', value_name='searches'),
x='date', y='searches', hue='term', ax=ax)
st.pyplot(fig)
elif selected_viz == "Heatmap":
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(st.session_state['data'].drop('date', axis=1).corr(), annot=True, cmap='coolwarm', ax=ax)
st.pyplot(fig)
elif selected_viz == "Bar Plot":
fig, ax = plt.subplots(figsize=(10, 6))
st.session_state['data'].drop('date', axis=1).mean().plot(kind='bar', ax=ax)
ax.set_ylabel('Average Search Interest')
st.pyplot(fig)
elif selected_viz == "Area Plot":
fig, ax = plt.subplots(figsize=(10, 6))
st.session_state['data'].set_index('date').plot.area(ax=ax)
ax.set_ylabel('Search Interest')
st.pyplot(fig)
elif selected_viz == "Violin Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.violinplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='term', y='searches', ax=ax)
st.pyplot(fig)
elif selected_viz == "Box Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.boxplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='term', y='searches', ax=ax)
st.pyplot(fig)
elif selected_viz == "Scatter Plot":
fig, ax = plt.subplots(figsize=(10, 6))
sns.scatterplot(data=st.session_state['data'].melt(id_vars='date', var_name='term', value_name='searches'),
x='date', y='searches', hue='term', ax=ax)
st.pyplot(fig)
elif selected_viz == "Pair Plot":
fig = sns.pairplot(st.session_state['data'].drop('date', axis=1), diag_kind='kde')
st.pyplot(fig)