Skip to main content

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

Project description

canonicalwebteam.search

circleci build status Code coverage PyPI version

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

Installation

pip3 install canonicalwebteam.search

Or add canonicalwebteam.search to your requirements.txt.

Usage

Local development

For local development, it's best to test this module with one of our website projects like ubuntu.com. For more information, follow this guide (internal only).

Application code

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

import talisker.requests
from flask import Flask
from canonicalwebteam.search 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

app.add_url_rule(
    "/docs/search",
    "docs-search",
    build_search_view(
        session=session,
        site="maas.io/docs",
        template_path="docs/search.html"
        search_engine_id="xxxxxxxxxx" # Optional argument, required by some of our sites
    )
)

Publish

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_APP=app.py 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.

For some sites, you will need to pass a specific search engine ID (cx) to the build_search_view which you will find in the Custom Search Engine page.

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.

Source Distribution

canonicalwebteam.search-1.2.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

canonicalwebteam.search-1.2.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file canonicalwebteam.search-1.2.0.tar.gz.

File metadata

File hashes

Hashes for canonicalwebteam.search-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2f6ec3b4de066558985c790edf32d5ebb87069909fc77ff572df53a380a504fe
MD5 a4157d64f041dfb6e020e64f74fc4146
BLAKE2b-256 0288e84e59658970891bbf52fd45b6e99304a250f07c26279290f23e0ed223c9

See more details on using hashes here.

File details

Details for the file canonicalwebteam.search-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam.search-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a5e0672b0ff60507ecc61a1c418320ee298912343b32d7aaa21a85e7cf6e43a
MD5 d40dd6471d873f0e5c9a85253aebab08
BLAKE2b-256 ef4a5be581459c0bc2815fe88cf2315bee5240ae1c440d7a85df48418edb54c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page