Flask-Avatars
All avatar generators in one place.
Installation
$ pip install flask-avatars
Initialization
The extension needs to be initialized in the usual way before it can be used:
from flask_avatars import Avatars
app = Flask(__name__)
avatars = Avatars(app)
Avatars
Flask-Avatars provide a avatars object in template context, you can use
it to get avatar URL.
Gravatar
You can use avatars.gravatar() to get an avatar URL provided by
Gravatar, pass the email
hash:
<img src="{{ avatars.gravatar(email_hash) }}">
You can get email hash like this:
import hashlib
avatar_hash = hashlib.md5(my_email.lower().encode('utf-8')).hexdigest()
Robohash
Robohash provide random robot avatar, you can use
avatars.robohash() to get the avatar URL, pass a random text:
<img src="{{ avatars.robohash(some_text) }}">
Avatars.io
Avatars.io let you use your social media's avatar
(Twitter, Facebook or Instagram), you can use avatars.social_media()
to get the avatar URL, pass your username on target social media:
<img src="{{ avatars.social_media(username) }}">
Default to use Twitter, use platform to change it:
<img src="{{ avatars.social_media(username, platform='facebook') }}">
Default avatar
Flask-Avatars provide a default avatar with three size, use avatars.default()
to get the URL:
<img src="{{ avatars.default() }}">
You can use size to change size (one of s, m and l), for example:
<img src="{{ avatars.default(size='s') }}">
Identicon generate
Flask-Avatars provide a Identicon class to generate identicon
avatar, most of the code was based on randomavatar.
First, you need set configuration variable AVATARS_SAVE_PATH to tell
Flask-Avatars the path to save generated avatars. Generally speaking, we
will generate avavar when the user record was created, so the best place to
generate avatar is in user database model class:
class User(db.Model):
avatar_s = db.Column(db.String(64))
avatar_m = db.Column(db.String(64))
avatar_l = db.Column(db.String(64))
def __init__():
generate_avatar()
def generate_avatar(self):
avatar = Identicon()
filenames = avatar.generate(text=self.username)
self.avatar_s = filenames[0]
self.avatar_m = filenames[1]
self.avatar_l = filenames[2]
db.session.commit()
Then create a view to serve avatar image like this:
from flask import send_form_directory, current_app
@app.route('/avatars/<path:filename>')
def get_avatar(filename):
return send_from_directory(current_app.config['AVATARS_SAVE_PATH'], filename)
Configuration
The configuration options available were listed below:
| Configuration | Default Value | Description |
|---|---|---|
| AVATARS_GRAVATAR_DEFAULT | identicon | Gravatar default avatar type |
| AVATARS_SAVE_PATH | None |
The path where avatar save |
| AVATARS_SIZE_TUPLE | (30, 60, 150) |
The avatar size tuple in a format of (small, medium, large), used when generate identicon avatar |
| AVATARS_IDENTICON_COLS | 7 | The cols of identicon avatar block |
| AVATARS_IDENTICON_ROWS | 7 | The ros of identicon avatar block |
| AVATARS_IDENTICON_BG | None |
The back ground color of identicaon avatar, pass RGB tuple (for example (125, 125, 125)). Default (None) to use random color |
TODO
- Fix English grammar error at everywhere :(
- Documentation
- Tests
- Example applications
ChangeLog
0.1.0
Release date: 2018/6/19
Initialize release.
License
This project is licensed under the MIT License (see the
LICENSE file for details).
Release files for Flask-Avatars 0.1.0
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-Avatars-0.1.0.tar.gz | 262.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_Avatars-0.1.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 544.3 kB
Release files / Flask-Avatars-0.1.0.tar.gz
| Download URL | Flask-Avatars-0.1.0.tar.gz |
|---|---|
| Size | 262.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d891b9b68aeee56976963194e276c171a3e2fa69af9ad1e0978aac3ceff55b5
|
|
BLAKE2b-256 checksum How to use checksums |
c39fc09484e01641ef1d1a13cf150fbd0890901ccd31a4411c363db4a1e4703e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / Flask_Avatars-0.1.0-py2.py3-none-any.whl
| Download URL | Flask_Avatars-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 281.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
47d4eb9ca3044132b3e97a03f92498284b9f6e462c38a99635ab88a00430fef5
|
|
BLAKE2b-256 checksum How to use checksums |
dbaef1f2850e2ad8dfc93693049019532188660098d7e67666971911d51bfe28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |