File size: 3,818 Bytes
de4288b 725655e 95eab5a d3dd66f 862ee7e 725655e d54ac9a 95eab5a d54ac9a d8b6c59 95eab5a d8b6c59 95eab5a d8b6c59 95eab5a 600e9aa 95eab5a 600e9aa d8b6c59 725655e 95eab5a 725655e 95eab5a 600e9aa 95eab5a 600e9aa 95eab5a 725655e 600e9aa 95eab5a d54ac9a 862ee7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
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.')
|