yashm commited on
Commit
600e9aa
·
verified ·
1 Parent(s): 725655e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -7
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 keyword
12
- keyword = st.text_input('Enter a keyword to analyze:', 'Streamlit')
13
 
14
- if keyword:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Fetch data from Google Trends
16
- pytrends.build_payload([keyword], cat=0, timeframe='today 12-m', geo='', gprop='')
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[keyword])
22
 
23
  # Display the dataframe
24
  st.write(interest_over_time_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  else:
26
- st.write('No data found for this keyword.')
27
 
28
- st.write('This is a simple Google Trends analysis app.')
 
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.')