blazingbunny commited on
Commit
c4b593a
·
1 Parent(s): 1730f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
  import requests
3
  import json
4
- import pprint
5
 
6
  # Function to fetch data from Google Knowledge Graph
7
- def fetch_data(query, api_key):
8
- url = f"https://kgsearch.googleapis.com/v1/entities:search?query={query}&key={api_key}&limit=1"
9
  response = requests.get(url)
10
  json_data = json.loads(response.text)
11
  return json_data
@@ -21,20 +21,26 @@ if not api_key:
21
  api_key = st.text_input('Enter your Google API Key:', type="password")
22
 
23
  search_query = st.text_input('Enter Search Query:', 'Python Programming')
 
24
 
25
  if st.button('Search'):
26
  if api_key:
27
- result = fetch_data(search_query, api_key)
28
  try:
29
- item = result['itemListElement'][0]['result']
30
- st.subheader(f"Name: {item['name']}")
31
- st.write(f"Description: {item.get('description', 'N/A')}")
32
- st.write(f"Detail: {item.get('detailedDescription', {}).get('articleBody', 'N/A')}")
33
- st.write(f"URL: {item.get('url', 'N/A')}")
34
-
35
- # Pretty-print the entire JSON response to display all outputs
36
- st.subheader("Full JSON Response")
37
- st.write(pprint.pformat(result))
 
 
 
 
 
38
  except:
39
  st.write('No results found.')
40
  else:
 
1
  import streamlit as st
2
  import requests
3
  import json
4
+ import pandas as pd
5
 
6
  # Function to fetch data from Google Knowledge Graph
7
+ def fetch_data(query, api_key, limit):
8
+ url = f"https://kgsearch.googleapis.com/v1/entities:search?query={query}&key={api_key}&limit={limit}"
9
  response = requests.get(url)
10
  json_data = json.loads(response.text)
11
  return json_data
 
21
  api_key = st.text_input('Enter your Google API Key:', type="password")
22
 
23
  search_query = st.text_input('Enter Search Query:', 'Python Programming')
24
+ output_count = st.slider('Number of Results:', min_value=1, max_value=10, value=1)
25
 
26
  if st.button('Search'):
27
  if api_key:
28
+ result = fetch_data(search_query, api_key, output_count)
29
  try:
30
+ items = result['itemListElement']
31
+ data = []
32
+ for item in items:
33
+ result_dict = {}
34
+ result_item = item['result']
35
+ result_dict['Name'] = result_item.get('name', 'N/A')
36
+ result_dict['Description'] = result_item.get('description', 'N/A')
37
+ result_dict['Detail'] = result_item.get('detailedDescription', {}).get('articleBody', 'N/A')
38
+ result_dict['URL'] = result_item.get('url', 'N/A')
39
+ data.append(result_dict)
40
+
41
+ # Convert list of dictionaries to DataFrame for table display
42
+ df = pd.DataFrame(data)
43
+ st.table(df)
44
  except:
45
  st.write('No results found.')
46
  else: