Skip to main content

Flask extension to provide a search view for querying the webteam's Google Custom Search account

Project description

circleci build status Code coverage PyPI version

Flask extension to provide a search view for querying the webteam's Google Custom Search account.


pip3 install

Or add to your requirements.txt.


Application code

You can add the extension on your project's application as follows:

from import build_search_view

app = Flask("myapp")
session = talisker.requests.get_session()  # You must provide a requests session

app.add_url_rule("/search", "search", build_search_view(session))

# Or, a bit more complex example


The template

You need to create an HTML template at the specificed template_path. By default this will be search.html inside your templates folder. This template will be passed the following data:

  • {{ query }} - the contents of the q= search query parameter
  • {{ start }} - the contents of the start= query parameter - the offset at which to start returning results (used for pagination - default 0)
  • {{ num }} - the contents of the num= query parameter - the number of search results to return (default 10)
  • {{ results }} - the results returned from the Google Custom Search query. The actual search results are in {{ results.entries }}.

The API key

You then need to provide the API key for the Google Custom Search API as an environment variable called SEARCH_API_KEY when the server starts - e.g.:

SEARCH_API_KEY=xxxxx flask run

Once this is done, you should be able to visit /search?q={some_query} in your site and see search results built with your search.html template.

New sites

If you created a new site and the API is not returning any results, you may need to add it to the Google Custom Search Engine list of sites. If you don't see any sites in this page, ask the Webteam.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size (10.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page