flask-selfheal 🔗
Automatically fix broken URLs in your Flask app using intelligent resolvers.
Features
- Multiple Matching Strategies: Choose from 1:1 exact matching, fuzzy matching, and even hybrid database-backed approaches to find the best matching URL.
- Configurable: Fine-tune matching strategies, thresholds, and normalization rules to suit your needs.
- Chainable Resolvers: Combine multiple resolvers to create a robust URL healing strategy.
- Easy Integration: Plug into any Flask app with minimal setup.
- Performant: Optimized to try fast methods first and only use expensive ones as a last resort.
Installation
uv pip install flask-selfheal
Usage
Fuzzy Flask Routes Resolver
This example demonstrates how to use the FlaskRoutesResolver to automatically correct common typos in your Flask routes using fuzzy matching (using difflib under the hood).
from flask import Flask
from flask_selfheal import SelfHeal
from flask_selfheal.resolvers import FlaskRoutesResolver
app = Flask(__name__)
@app.route("/home")
def home():
return "Welcome Home!"
@app.route("/about")
def about():
return "About Us"
@app.route("/contact")
def contact():
return "Contact Us"
# Configure SelfHeal with fuzzy matching
SelfHeal(app, resolvers=[FlaskRoutesResolver()])
if __name__ == "__main__":
app.run(debug=True)
https://example.com/hme --> Redirects to /home
https://example.com/abot --> Redirects to /about
https://example.com/contat --> Redirects to /contact
Chaining Multiple Resolvers
You can combine multiple resolvers to create a more robust URL healing strategy. In this example, we use both AliasMappingResolver and FuzzyMappingResolver to handle obsolete URLs and common typos.
from flask import Flask
from flask_selfheal import SelfHeal
from flask_selfheal.resolvers import AliasMappingResolver, FuzzyMappingResolver
app = Flask(__name__)
@app.route("/home")
def home():
return "Welcome Home!"
@app.route("/new-path")
def new_path():
return "This is the new path!"
# Define resolvers - will be tried in order defined
resolvers = [
AliasMappingResolver(
{"old-path": "new-path"} # Handle obsolete URLs
),
FuzzyMappingResolver(
["home", "new-path"] # Handle typos
)
]
SelfHeal(app, resolvers=resolvers)
if __name__ == "__main__":
app.run(debug=True)
https://example.com/old-path --> Redirects to /new-path
https://example.com/hme --> Redirects to /home
https://example.com/new-pth --> Redirects to /new-path
Database-Backed Resolver
The DatabaseResolver allows you to resolve URLs based on the slug in your database (using SQLAlchemy).
Ensure that you have a model with a slug field in your database (example below).
class Articles(db.Model):
id = db.Column(db.Integer, primary_key=True)
slug = db.Column(db.String(100), unique=True, nullable=False) # <--
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
from flask import Flask
from app.models import Articles # Your SQLAlchemy model
from flask_selfheal import SelfHeal
from flask_selfheal.resolvers import DatabaseResolver
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///yourdatabase.db"
db.init_app(app)
@app.route("/articles/<slug>")
def article_detail(slug):
article = Articles.query.filter_by(slug=slug).first_or_404()
return f"Article: {article.title}"
# Configure the DatabaseResolver
db_resolver = [
DatabaseResolver(
Articles,
slug_field='slug',
)
]
SelfHeal(app, resolvers=db_resolver, redirect_pattern="/articles/{slug}")
if __name__ == "__main__":
app.run(debug=True)
https://example.com/articles/1234 --> Redirects to /articles/hello-world-1234567
https://example.com/articles/hello-world --> Redirects to /articles/hello-world-1234567
https://example.com/articles/hell-wrl --> Redirects to /articles/hello-world-1234567
https://example.com/articles/world --> Redirects to /articles/hello-world-1234567
The DatabaseResolver follows these strategies in order when trying to find a matching slug:
- Exact match: Fastest, direct database lookup
- SQL
LIKEmatching: Handle simple variations using SQLLIKEwildcards - Normalized matching: Handle common typos (
0 -> o,1 -> l, etc.) - Word-based matching: Match individual significant words
- Partial matching: Match meaningful substrings
- Fuzzy matching: Handle more complex typos by fuzzy matching
Fine-tuning the Database Resolver
The DatabaseResolver can be customized with various parameters to adjust its behavior:
resolver = DatabaseResolver(
model=Product,
slug_field='slug',
use_fuzzy=True, # Enable/disable last resort fuzzy matching
fuzzy_cutoff=0.7, # Similarity threshold (0-1)
enable_word_matching=True, # Match individual words
enable_partial_matching=True, # Match partial strings
min_word_length=3, # Minimum word length to consider
custom_normalizers={ # Custom normalizer mappings (0 -> o, ph -> f, etc.)
'0': 'o', 'ph': 'f'
}
)
You can take a look at more examples in the examples/ directory
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Metadata
Release files for flask-selfheal 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask_selfheal-1.1.2.tar.gz | 7.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_selfheal-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / flask_selfheal-1.1.2.tar.gz
| Download URL | flask_selfheal-1.1.2.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19c286c25c3f1e95d6c7437039d917ebc118ba3785f140646f188f98d734cd8c
|
|
BLAKE2b-256 checksum How to use checksums |
e2d3730d549d78749e54efbf4fcd8b4af82e8ac1aa14fceb311ab2048351b86b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / flask_selfheal-1.1.2-py3-none-any.whl
| Download URL | flask_selfheal-1.1.2-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1e31f47a3597f6caef742ad47ad1d42f2dcdccf2f387f2099db52afc10d9f34
|
|
BLAKE2b-256 checksum How to use checksums |
c3b68737ef121153edc7661ff98181c02e11877add3e899ff817eb6004f169d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|