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
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 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.
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
Hashes for canonicalwebteam.search-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e655dda22ebcf02f05810b6dd12f5a8a0c9f0a5f2a1652e6e8b13ff32407ec5c |
|
MD5 | 6b5439d6f2c25435650e43aada4629a5 |
|
BLAKE2b-256 | e4a076c2ad8e05d6c510b9addc67193e8780339da708601d72017af7771a1b2a |