Flask extension to provide a search view for querying the webteam's Google Custom Search account
Project description
canonicalwebteam.search
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
)
)
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 theq=
search query parameter{{ start }}
- the contents of thestart=
query parameter - the offset at which to start returning results (used for pagination - default 0){{ num }}
- the contents of thenum=
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file canonicalwebteam.search-1.2.4.tar.gz
.
File metadata
- Download URL: canonicalwebteam.search-1.2.4.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec4b77b67cb7593030d38899a1b0cd497a4b8019de02b1dfab2b13844f854cfc |
|
MD5 | 0feae3da9f406de36deedbcfd51d9a70 |
|
BLAKE2b-256 | 39e60a16d5b8cebb78ae196f4f33bb57bda4bae32e410ce8284a2ff77028b5af |
File details
Details for the file canonicalwebteam.search-1.2.4-py3-none-any.whl
.
File metadata
- Download URL: canonicalwebteam.search-1.2.4-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8f14010a03fdb38b5255318e5b62a7f462f7d0368393b6f66a26e71c75f0592 |
|
MD5 | 0f8650b8eb344bba2fdc07ecac198ba8 |
|
BLAKE2b-256 | 82ae88520e69ce01ad962ebf894318e880b0d93b55e7d80551da965c35fa8d0c |