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

Application code

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

from canonicalwebteam.search import build_search_view

app = Flask("myapp")

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

# Or, a bit more complex example

app.add_url_rule(
    "/docs/search",
    "docs-search",
    build_search_view(
        site="maas.io/docs",
        template_path="docs/search.html"
    )
)

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.

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-0.2.1.tar.gz (10.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: canonicalwebteam.search-0.2.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.8.0 tqdm/4.32.1 CPython/3.6.9

File hashes

Hashes for canonicalwebteam.search-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d186963a0cec42f8a28b2b99f6f0510c0a1d05fc8b33979888c019b5dfa65b63
MD5 842b32a0aea048e3a696fc6a3a985d6a
BLAKE2b-256 331e1e6d3d1ddc5c795a5a378aabe313ab1054b4dd42bd335a4e1b107c9a9fe7

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