Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,69 @@ import streamlit as st
|
|
| 2 |
from pytrends.request import TrendReq
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
|
| 6 |
# Set up the pytrends API
|
| 7 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 8 |
|
| 9 |
st.title('Google Trends Analyzer')
|
| 10 |
|
| 11 |
-
# Input for
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Fetch data from Google Trends
|
| 16 |
-
pytrends.build_payload(
|
| 17 |
interest_over_time_df = pytrends.interest_over_time()
|
| 18 |
|
| 19 |
if not interest_over_time_df.empty:
|
| 20 |
# Display data as a line chart
|
| 21 |
-
st.line_chart(interest_over_time_df[
|
| 22 |
|
| 23 |
# Display the dataframe
|
| 24 |
st.write(interest_over_time_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
else:
|
| 26 |
-
st.write('No data found for
|
| 27 |
|
| 28 |
-
st.write('This is
|
|
|
|
| 2 |
from pytrends.request import TrendReq
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from pytrendsexpander import pytrendsExpander
|
| 6 |
|
| 7 |
# Set up the pytrends API
|
| 8 |
pytrends = TrendReq(hl='en-US', tz=360)
|
| 9 |
|
| 10 |
st.title('Google Trends Analyzer')
|
| 11 |
|
| 12 |
+
# Input for multiple keywords
|
| 13 |
+
keywords = st.text_input('Enter keywords to analyze (comma-separated):', 'Streamlit, Data Science')
|
| 14 |
|
| 15 |
+
# Geographical focus
|
| 16 |
+
geo = st.text_input('Enter geographical region (e.g., US, FR, TH):', '')
|
| 17 |
+
|
| 18 |
+
# Time range selection
|
| 19 |
+
timeframe = st.selectbox('Select Time Range:',
|
| 20 |
+
['today 12-m', 'today 3-m', 'today 1-m', 'today 5-y', 'all'])
|
| 21 |
+
|
| 22 |
+
# Category selection
|
| 23 |
+
category = st.text_input('Enter category number (default is 0):', '0')
|
| 24 |
+
|
| 25 |
+
# Data Source selection
|
| 26 |
+
data_source = st.selectbox('Select Data Source:',
|
| 27 |
+
['Web Search', 'Image Search', 'News Search', 'Google Shopping', 'YouTube Search'])
|
| 28 |
+
|
| 29 |
+
if keywords:
|
| 30 |
+
# Prepare the keywords list
|
| 31 |
+
kw_list = [kw.strip() for kw in keywords.split(',')]
|
| 32 |
+
|
| 33 |
+
# Convert data source to pytrends parameter
|
| 34 |
+
gprop_map = {
|
| 35 |
+
'Web Search': '',
|
| 36 |
+
'Image Search': 'images',
|
| 37 |
+
'News Search': 'news',
|
| 38 |
+
'Google Shopping': 'froogle',
|
| 39 |
+
'YouTube Search': 'youtube'
|
| 40 |
+
}
|
| 41 |
+
gprop = gprop_map[data_source]
|
| 42 |
+
|
| 43 |
# Fetch data from Google Trends
|
| 44 |
+
pytrends.build_payload(kw_list, cat=int(category), timeframe=timeframe, geo=geo, gprop=gprop)
|
| 45 |
interest_over_time_df = pytrends.interest_over_time()
|
| 46 |
|
| 47 |
if not interest_over_time_df.empty:
|
| 48 |
# Display data as a line chart
|
| 49 |
+
st.line_chart(interest_over_time_df[kw_list])
|
| 50 |
|
| 51 |
# Display the dataframe
|
| 52 |
st.write(interest_over_time_df)
|
| 53 |
+
|
| 54 |
+
# Download option for the dataframe
|
| 55 |
+
csv = interest_over_time_df.to_csv().encode('utf-8')
|
| 56 |
+
st.download_button(label="Download data as CSV", data=csv, file_name='google_trends_data.csv', mime='text/csv')
|
| 57 |
+
|
| 58 |
+
# Embedding the chart
|
| 59 |
+
st.write('Share or embed the chart by copying the URL.')
|
| 60 |
+
st.write(st.text_area("URL for embedding", value=st.get_url(), height=100))
|
| 61 |
+
|
| 62 |
+
# Use pytrendsExpander for network analysis
|
| 63 |
+
st.subheader('Related Queries and Topics')
|
| 64 |
+
expander = pytrendsExpander(kw_list[0])
|
| 65 |
+
related_df = expander.expand_search(5)
|
| 66 |
+
st.write(related_df)
|
| 67 |
else:
|
| 68 |
+
st.write('No data found for these keywords.')
|
| 69 |
|
| 70 |
+
st.write('This is an enhanced Google Trends analysis app.')
|