An extension to facilitate using FTSCursor with flask
Project description
Flask-FTSCursor
An extension to facilitate using FTSCursor with Flask
Installation
pip3 install flask-ftscursor
Mission Statement
This package was inspired by Miguel Grinberg's Flask Mega-Tutorial, specifically Chapter 16: Full-Text Search. Even more specifically, it was this sentence:
Using the searching capabilities of one of the relational databases could also be a good choice, but given the fact that SQLAlchemy does not support this functionality, I would have to handle the searching with raw SQL statements, or else find a package that provides high-level access to text searches while being able to coexist with SQLAlchemy.
Flask-FTSCursor is such a package. It provides high-level access to SQLite3 full-text searches while being able to coexist harmoniously with SQLAlchemy.
Tutorial
import sqlite3
from flask import Flask
from flask-ftscursor import FTS
fts = FTS()
def create_app():
app = Flask(__name__)
app.config['FTS_DATABASE'] = 'fts.db'
app.config['FTS_SOURCE_DATABASE'] = 'app.db'
fts.init_app(app)
return app
app = create_app()
conn = sqlite3.connect(app.config['FTS_SOURCE_DATABASE'])
c = conn.cursor()
c.executescript('''
CREATE TABLE my_table(id INTEGER, body TEXT);
INSERT INTO my_table(id, body) VALUES
(1, 'this is a test'),
(2, 'a second test');
'''
)
conn.commit()
app.fts.search(table='my_table', query='this test', page=1, per_page=2)
app.fts.search(table='my_table', query='second', page=1, per_page=2)
with app.app_context()
app.fts.index(table='my_table', id=1, searchable=('body',))
app.fts.index(table='my_table', id=2, searchable=('body',))
app.fts.search(table='my_table', query='this test', page=1, per_page=2)
app.fts.search(table='my_table', query='second', page=1, per_page=2)
app.fts.drop(table='my_table')
Configuration
Flask-FTSCursor relies on two items in the app's configuration: FTS_DATABASE
and FTS_SOURCE_DATABASE
.
The value of FTS_SOURCE_DATABASE
should be the file path of the app's main
SQLite3 database, or whichever database contains the entries that will be
indexed. The value of FTS_DATABSE
should be the file path where the database
containing the FTS tables will be kept.
Helper Functions
Flask-FTSCursor provides functions named add_to_index()
,
remove_from_index()
, and query_index()
which can be used in place of the
similarly named functions given in Miguel Grinberg's Flask Mega-Tutorial,
Chapter 16: Full-Text Search,
under the section titled "A Full-Text Search Abstraction." For an example of
an app that uses these functions, see ucsd-bisb-unofficial.
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
Hashes for Flask_FTSCursor-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1d959c755e2aadd4b9fb030bef9345489a0261b56d6dfb7bf685557594e6cf8 |
|
MD5 | 3b8c1f17eba2bac57c80caf72b8f7615 |
|
BLAKE2b-256 | 4dfd28dc408edf31aceca48ee7f1371e32097a3092d17f0f9ff20cefeef0ee9c |