Skip to main content

Hashids integration for Flask applications.

Project description

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:

Examples

from flask import abort, Flask, render_template, url_for
from flask_hashids import HashidMixin, Hashids
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
# The SECRET_KEY is used as a salt, so don't forget to set this in production
app.config['SECRET_KEY'] = 'secret!'
db = SQLAlchemy(app)
hashids = Hashids(app)


class User(HashidMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)


@app.route('/users')
def users():
    hashid_users = [
      {
        'id': user.hashid,  # hashid property from HashidMixin
        'name': user.name,
        'url': url_for('user', user_id=user.id)  # Int id for url generation
      } for user in User.query.all()
    ]
    return render_template('users.html', users=hashid_users)


@app.route('/users/<hashid:user_id>')
def user(user_id):
    # The HashidConverter decodes the given hashid to an int
    user = User.query.get_or_404(user_id):
    return render_template('user.html', user=user)


if __name__ == '__main__':
    app.run()

Ressources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Hashids-0.2.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

Flask_Hashids-0.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Hashids-0.2.0.tar.gz.

File metadata

  • Download URL: Flask-Hashids-0.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for Flask-Hashids-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e28116fec5166c5eb882252f31aad3c0bf88666e3939452c966ec00b50f75c2b
MD5 d06517d4ea7910bd7cf75f360657f529
BLAKE2b-256 601d3d8b59f2aec11781071191deddebeb03edcd9e99850aa63096e10f994708

See more details on using hashes here.

File details

Details for the file Flask_Hashids-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: Flask_Hashids-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for Flask_Hashids-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f940be5ffb8d0fffb69777c863130bb8b1d5d0bab803822373d1ad9a8b96614
MD5 e9e9d733673e9dc2607f15272bd87e2b
BLAKE2b-256 95127bd8acaad6573fb9b499557925f04820fcbf5065c1822844e5164fe28b2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page