Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import streamlit as st
|
|
| 2 |
from pytrends.request import TrendReq
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class pytrendsExpander:
|
| 7 |
def __init__(self, keyword):
|
|
@@ -11,82 +14,108 @@ class pytrendsExpander:
|
|
| 11 |
|
| 12 |
def expand_search(self, m):
|
| 13 |
pytrends = self.pytrends
|
| 14 |
-
|
| 15 |
-
# Get seed keyword
|
| 16 |
kw_list = [self.keyword]
|
| 17 |
pytrends.build_payload(kw_list, cat=0, timeframe='today 1-m', geo='US', gprop='')
|
| 18 |
-
|
| 19 |
-
# Get results from the seed keyword
|
| 20 |
queries = pytrends.related_queries()
|
| 21 |
-
|
| 22 |
-
# Store the results in the data frame
|
| 23 |
df_results = pd.DataFrame(queries.get(self.keyword).get('top').rename_axis('ranking').reset_index())
|
| 24 |
-
|
| 25 |
return df_results.head(m)
|
| 26 |
|
| 27 |
-
# Set up the pytrends API
|
| 28 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 29 |
|
| 30 |
-
st.
|
|
|
|
| 31 |
|
| 32 |
-
# Sidebar for options
|
| 33 |
with st.sidebar:
|
| 34 |
st.header('Analysis Options')
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
keyword2 = st.text_input('Enter second keyword to analyze (optional):', '')
|
| 39 |
-
keyword3 = st.text_input('Enter third keyword to analyze (optional):', '')
|
| 40 |
-
|
| 41 |
-
# Geographical focus dropdown
|
| 42 |
countries = ['US', 'FR', 'TH', 'DE', 'IN', 'JP', 'BR', 'GB', 'CA', 'AU']
|
| 43 |
geo = st.selectbox('Select geographical region:', countries)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
timeframes = ['now 1-H', 'now 5-H', 'now 1-d', 'now 7-d', 'today 1-m', 'today 3-m']
|
| 47 |
timeframe = st.selectbox('Select Time Range:', timeframes)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
data_source = st.selectbox('Select Data Source:',
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
gprop_map = {
|
| 59 |
-
'Web Search': '',
|
| 60 |
-
'
|
| 61 |
-
'YouTube Search': 'youtube'
|
| 62 |
}
|
| 63 |
gprop = gprop_map[data_source]
|
| 64 |
|
| 65 |
-
# Fetch data from Google Trends
|
| 66 |
pytrends.build_payload(kw_list, cat=0, timeframe=timeframe, geo=geo, gprop=gprop)
|
| 67 |
interest_over_time_df = pytrends.interest_over_time()
|
| 68 |
-
|
| 69 |
if not interest_over_time_df.empty:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
st.write(interest_over_time_df)
|
| 75 |
-
|
| 76 |
-
# Download option for the dataframe
|
| 77 |
csv = interest_over_time_df.to_csv().encode('utf-8')
|
| 78 |
st.download_button(label="Download data as CSV", data=csv, file_name='google_trends_data.csv', mime='text/csv')
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# Use pytrendsExpander for network analysis
|
| 85 |
st.subheader('Related Queries and Topics')
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
st.write('No data found for these keywords.')
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
st.write('This is an
|
|
|
|
| 2 |
from pytrends.request import TrendReq
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
from wordcloud import WordCloud
|
| 8 |
|
| 9 |
class pytrendsExpander:
|
| 10 |
def __init__(self, keyword):
|
|
|
|
| 14 |
|
| 15 |
def expand_search(self, m):
|
| 16 |
pytrends = self.pytrends
|
|
|
|
|
|
|
| 17 |
kw_list = [self.keyword]
|
| 18 |
pytrends.build_payload(kw_list, cat=0, timeframe='today 1-m', geo='US', gprop='')
|
|
|
|
|
|
|
| 19 |
queries = pytrends.related_queries()
|
|
|
|
|
|
|
| 20 |
df_results = pd.DataFrame(queries.get(self.keyword).get('top').rename_axis('ranking').reset_index())
|
|
|
|
| 21 |
return df_results.head(m)
|
| 22 |
|
|
|
|
| 23 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 24 |
|
| 25 |
+
st.set_page_config(layout="wide")
|
| 26 |
+
st.title('Advanced Google Trends Analyzer')
|
| 27 |
|
|
|
|
| 28 |
with st.sidebar:
|
| 29 |
st.header('Analysis Options')
|
| 30 |
+
keywords = st.text_area('Enter keywords to analyze (one per line):', 'Streamlit')
|
| 31 |
+
kw_list = [kw.strip() for kw in keywords.split('\n') if kw.strip()]
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
countries = ['US', 'FR', 'TH', 'DE', 'IN', 'JP', 'BR', 'GB', 'CA', 'AU']
|
| 34 |
geo = st.selectbox('Select geographical region:', countries)
|
| 35 |
+
|
| 36 |
+
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']
|
|
|
|
| 37 |
timeframe = st.selectbox('Select Time Range:', timeframes)
|
| 38 |
+
|
| 39 |
+
data_sources = ['Web Search', 'Image Search', 'YouTube Search', 'News Search', 'Google Shopping']
|
| 40 |
+
data_source = st.selectbox('Select Data Source:', data_sources)
|
| 41 |
+
|
| 42 |
+
chart_type = st.selectbox('Select Chart Type:', ['Line', 'Area', 'Bar'])
|
| 43 |
+
|
| 44 |
+
advanced_options = st.expander('Advanced Options')
|
| 45 |
+
with advanced_options:
|
| 46 |
+
normalize = st.checkbox('Normalize Data', value=True)
|
| 47 |
+
moving_average = st.checkbox('Apply Moving Average')
|
| 48 |
+
if moving_average:
|
| 49 |
+
ma_window = st.slider('Moving Average Window', 1, 30, 7)
|
| 50 |
+
|
| 51 |
+
if kw_list:
|
| 52 |
gprop_map = {
|
| 53 |
+
'Web Search': '', 'Image Search': 'images', 'YouTube Search': 'youtube',
|
| 54 |
+
'News Search': 'news', 'Google Shopping': 'froogle'
|
|
|
|
| 55 |
}
|
| 56 |
gprop = gprop_map[data_source]
|
| 57 |
|
|
|
|
| 58 |
pytrends.build_payload(kw_list, cat=0, timeframe=timeframe, geo=geo, gprop=gprop)
|
| 59 |
interest_over_time_df = pytrends.interest_over_time()
|
| 60 |
+
|
| 61 |
if not interest_over_time_df.empty:
|
| 62 |
+
st.subheader('Interest Over Time')
|
| 63 |
+
|
| 64 |
+
if moving_average:
|
| 65 |
+
for col in kw_list:
|
| 66 |
+
interest_over_time_df[f'{col}_MA'] = interest_over_time_df[col].rolling(window=ma_window).mean()
|
| 67 |
+
plot_columns = [f'{col}_MA' for col in kw_list]
|
| 68 |
+
else:
|
| 69 |
+
plot_columns = kw_list
|
| 70 |
+
|
| 71 |
+
if chart_type == 'Line':
|
| 72 |
+
fig = px.line(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
|
| 73 |
+
elif chart_type == 'Area':
|
| 74 |
+
fig = px.area(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
|
| 75 |
+
else: # Bar
|
| 76 |
+
fig = px.bar(interest_over_time_df, x=interest_over_time_df.index, y=plot_columns)
|
| 77 |
+
|
| 78 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 79 |
+
|
| 80 |
+
st.subheader('Data Table')
|
| 81 |
st.write(interest_over_time_df)
|
| 82 |
+
|
|
|
|
| 83 |
csv = interest_over_time_df.to_csv().encode('utf-8')
|
| 84 |
st.download_button(label="Download data as CSV", data=csv, file_name='google_trends_data.csv', mime='text/csv')
|
| 85 |
|
| 86 |
+
st.subheader('Correlation Heatmap')
|
| 87 |
+
corr = interest_over_time_df[kw_list].corr()
|
| 88 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
| 89 |
+
sns.heatmap(corr, annot=True, cmap='coolwarm', ax=ax)
|
| 90 |
+
st.pyplot(fig)
|
| 91 |
|
|
|
|
| 92 |
st.subheader('Related Queries and Topics')
|
| 93 |
+
for keyword in kw_list:
|
| 94 |
+
st.write(f"Related to '{keyword}':")
|
| 95 |
+
expander = pytrendsExpander(keyword)
|
| 96 |
+
related_df = expander.expand_search(10)
|
| 97 |
+
st.write(related_df)
|
| 98 |
+
|
| 99 |
+
# Generate and display word cloud
|
| 100 |
+
wordcloud = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(
|
| 101 |
+
dict(zip(related_df['query'], related_df['value']))
|
| 102 |
+
)
|
| 103 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 104 |
+
ax.imshow(wordcloud, interpolation='bilinear')
|
| 105 |
+
ax.axis('off')
|
| 106 |
+
st.pyplot(fig)
|
| 107 |
+
|
| 108 |
+
st.subheader('Geographical Interest')
|
| 109 |
+
interest_by_region = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=True)
|
| 110 |
+
fig = px.choropleth(interest_by_region, locations=interest_by_region.index,
|
| 111 |
+
color=kw_list[0], # You can allow users to select which keyword to display
|
| 112 |
+
hover_name=interest_by_region.index,
|
| 113 |
+
color_continuous_scale=px.colors.sequential.Plasma)
|
| 114 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 115 |
+
|
| 116 |
else:
|
| 117 |
st.write('No data found for these keywords.')
|
| 118 |
+
else:
|
| 119 |
+
st.write('Please enter at least one keyword to analyze.')
|
| 120 |
|
| 121 |
+
st.write('This is an advanced Google Trends analysis app.')
|