OpenTrend / app.py
yashm's picture
Update app.py
862ee7e verified
import streamlit as st
from pytrends.request import TrendReq
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from wordcloud import WordCloud
# Initialize pytrends
pytrends = TrendReq(hl='en-US', tz=360)
st.set_page_config(layout="wide")
st.title('Advanced Google Trends Analyzer')
with st.sidebar:
st.header('Analysis Options')
keywords = st.text_area('Enter keywords to analyze (one per line):', 'Streamlit')
kw_list = [kw.strip() for kw in keywords.split('\n') if kw.strip()]
countries = ['US', 'FR', 'TH', 'DE', 'IN', 'JP', 'BR', 'GB', 'CA', 'AU']
geo = st.selectbox('Select geographical region:', countries)
timeframes = ['now 1-H', 'now 4-H', 'now 1-d', 'now 7-d', 'today 1-m', 'today 3-m', 'today 12-m', 'today 5-y']
timeframe = st.selectbox('Select Time Range:', timeframes)
data_sources = ['Web Search', 'Image Search', 'YouTube Search', 'News Search', 'Google Shopping']
data_source = st.selectbox('Select Data Source:', data_sources)
chart_type = st.selectbox('Select Chart Type:', ['Line', 'Area', 'Bar'])
advanced_options = st.expander('Advanced Options')
with advanced_options:
normalize = st.checkbox('Normalize Data', value=True)
moving_average = st.checkbox('Apply Moving Average')
if moving_average:
ma_window = st.slider('Moving Average Window', 1, 30, 7)
if kw_list:
gprop_map = {
'Web Search': '', 'Image Search': 'images', 'YouTube Search': 'youtube',
'News Search': 'news', 'Google Shopping': 'froogle'
}
gprop = gprop_map[data_source]
pytrends.build_payload(kw_list, cat=0, timeframe=timeframe, geo=geo, gprop=gprop)
interest_over_time_df = pytrends.interest_over_time()
if not interest_over_time_df.empty:
st.subheader('Interest Over Time')
if moving_average:
for col in kw_list:
interest_over_time_df[f'{col}_MA'] = interest_over_time_df[col].rolling(window=ma_window).mean()
plot_columns = [f'{col}_MA' for col in kw_list]
else:
plot_columns = kw_list
if chart_type == 'Line':
fig = px.line(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
elif chart_type == 'Area':
fig = px.area(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
else: # Bar
fig = px.bar(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
st.plotly_chart(fig, use_container_width=True)
csv = interest_over_time_df.to_csv().encode('utf-8')
st.download_button(label="Download data as CSV", data=csv, file_name='google_trends_data.csv', mime='text/csv')
st.subheader('Correlation Heatmap')
corr = interest_over_time_df[kw_list].corr()
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(corr, annot=True, cmap='coolwarm', ax=ax)
st.pyplot(fig)
st.subheader('Geographical Interest')
interest_by_region = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=True)
fig = px.choropleth(interest_by_region, locations=interest_by_region.index,
color=kw_list[0], # You can allow users to select which keyword to display
hover_name=interest_by_region.index,
color_continuous_scale=px.colors.sequential.Plasma)
st.plotly_chart(fig, use_container_width=True)
else:
st.write('No data found for these keywords.')
else:
st.write('Please enter at least one keyword to analyze.')
st.write('This is an advanced Google Trends analysis app.')