Skip to main content

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

Requirements

If you are using Flask-Base you need to be using atleast 2.0.0. If you are not, you will need atleast Flask@2.3.0.

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") # You must provide app
session = talisker.requests.get_session()  # You must provide a requests session

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

# Or, a bit more complex example

app.add_url_rule(
    "/docs/search",
    "docs-search",
    build_search_view(
        app=app # required in version 1.4.0 onwards
        session=session,
        site="maas.io/docs",
        template_path="docs/search.html",
        search_engine_id="xxxxxxxxxx", # Optional argument, required by some of our sites
        request_limit="500/day", # Allows your to configure the limit at which the user will be forbidden to query more. If omitted defaults to 2/second, 100/minute, 2000/day
    )
)

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.

Metadata

Release files for canonicalwebteam.search 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for canonicalwebteam.search 2.1.2
File Size Uploaded
canonicalwebteam_search-2.1.2.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for canonicalwebteam.search 2.1.2
File Interpreter ABI Platform
canonicalwebteam_search-2.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.2 kB

Release files / canonicalwebteam_search-2.1.2.tar.gz

Download URL canonicalwebteam_search-2.1.2.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7059541685b85be15b262d64a6154ac4386df7f73e98ba51a79fb82b73b42f24
BLAKE2b-256 checksum
How to use checksums
e5af9f65172a23343c4679ebee2f2893a2bb042fff5a07b0d47bc1567bcc54a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / canonicalwebteam_search-2.1.2-py3-none-any.whl

Download URL canonicalwebteam_search-2.1.2-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
798b54adf7e4dbe451301b4b1dd8e84c116b9fda5d57d1f4570fee898e4b262a
BLAKE2b-256 checksum
How to use checksums
b0d9273e4aabf540784ed0ce839c8a9f83caea673eac0dffbd7b12f50dc35702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page