Flask-Hashids
Hashids integration for Flask applications, it is based on the Hashid package available on PyPi. With this extension you can conveniently use integer ids for your application logic or database tables and hash them before exposing it in URLs or JSON data.
Installation
The latest stable version is available on PyPI. Either add Flask-Hashids to your requirements.txt file or install with pip:
pip install Flask-Hashids
Configuration
Flask-Hashids is configured through the standard Flask config API. These are the available options:
- HASHIDS_ALPHABET: Read more about that in Hashids documentation
- HASHIDS_MIN_LENGTH: Read more about that in Hashids documentation
- HASHIDS_SALT: It is strongly recommended that this is NEVER the same as the
SECRET_KEY. Read more about this option in Hashids documentation
Examples
You can find more detailed examples on how to use Flask-Hashids in the examples directory.
from flask import Flask, jsonify, url_for
from flask_hashids import HashidMixin, Hashids
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'top_secret_key'
app.config['HASHIDS_SALT'] = 'secret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
db = SQLAlchemy(app)
hashids = Hashids(app)
# The HashidMixin class adds the hashid property which will compute a hashid
# based on the existing id attribute of the instance.
# NOTE: The id attribute must be an int
class User(HashidMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
@property
def url(self):
# The HashidConverter encodes the given id to a hashid in the URL
return url_for('user', user_id=self.id)
def to_json_serializeable(self):
return {'id': self.hashid, 'name': self.name, 'url': self.url}
@app.before_first_request
def database_setup():
db.create_all()
john = User(name='John')
db.session.add(john)
jane = User(name='Jane')
db.session.add(jane)
db.session.commit()
@app.route('/users')
def users():
return [user.to_json_serializeable() for user in User.query.all()], 200
@app.route('/users/<hashid:user_id>')
def user(user_id: int):
# The HashidConverter decodes the given hashid to an int
user = User.query.get(user_id)
if user is None:
return jsonify('User not found'), 404
return user.to_json_serializeable(), 200
def main():
# You can encode and decode hashids manually
id = 123
encoded_id = hashids.encode(id)
decoded_id = hashids.decode(encoded_id)
app.run()
if __name__ == '__main__':
main()
Resources
Metadata
Release files for Flask-Hashids 1.0.3
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-Hashids-1.0.3.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_Hashids-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.8 kB
Release files / Flask-Hashids-1.0.3.tar.gz
| Download URL | Flask-Hashids-1.0.3.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e86696841f760e0da6875bcbfd2d3118fb3e49ff91fd4b488f55bbeb66ccee20
|
|
BLAKE2b-256 checksum How to use checksums |
7f66951af2400eed570d1d9e9c585f97a61de0a054f50ee998d1cb4339d55240
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.12
|
Release files / Flask_Hashids-1.0.3-py3-none-any.whl
| Download URL | Flask_Hashids-1.0.3-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9787d4eb37e8b4971cd530da7bc6a217701ad6227e0b0771f8d1fdf5cc1c5189
|
|
BLAKE2b-256 checksum How to use checksums |
bb7aab541c0aed42e7ac46699941b1bf824b5ff6f8ddb6ee52a17d15cc6aa2f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.12
|