A typeahead search using Google
To upload files, please first save the app
import streamlit as st
import requests
from bs4 import BeautifulSoup
import json
st.title("Google Search Typeahead")
# Function to get search suggestions from Google
def get_suggestions(query):
if not query:
return []
# NOTE: Using corsproxy.io because we're in a WASM environment. If running locally,
# you can remove the corsproxy.io prefix.
url = f"https://corsproxy.io/?https://suggestqueries.google.com/complete/search?client=firefox&q={query}"
try:
response = requests.get(url)
suggestions = response.json()[1] # Google returns suggestions in second element
return suggestions
except Exception as e:
st.error(f"Error fetching suggestions: {str(e)}")
return []
# Create a text input for search
search_query = st.text_input("Enter your search query:", key="search")
# Get and display suggestions as the user types
if search_query:
suggestions = get_suggestions(search_query)
if suggestions:
st.write("Search suggestions:")
for suggestion in suggestions:
st.write(f"▪ {suggestion}")
else:
st.write("No suggestions found.")
st.markdown("""
---
This app demonstrates a Google search typeahead functionality. As you type in the search box,
it fetches real-time suggestions from Google's suggestion API. The suggestions are updated
with each keystroke.
Note: This is using Google's public suggestion API which is typically used by browsers.
The results are the same as what you'd see when typing in Google's search box.
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?