Skip to main content

All avatar generators in one place.

Project description

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).

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-Avatars-0.1.0.tar.gz (262.6 kB view details)

Uploaded Source

Built Distribution

Flask_Avatars-0.1.0-py2.py3-none-any.whl (281.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Flask-Avatars-0.1.0.tar.gz.

File metadata

  • Download URL: Flask-Avatars-0.1.0.tar.gz
  • Upload date:
  • Size: 262.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Flask-Avatars-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d891b9b68aeee56976963194e276c171a3e2fa69af9ad1e0978aac3ceff55b5
MD5 9625b3f2cdebb6c3664c34970832335e
BLAKE2b-256 c39fc09484e01641ef1d1a13cf150fbd0890901ccd31a4411c363db4a1e4703e

See more details on using hashes here.

File details

Details for the file Flask_Avatars-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Avatars-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 47d4eb9ca3044132b3e97a03f92498284b9f6e462c38a99635ab88a00430fef5
MD5 ada8611ff779b49ddbe9ad541fbdcbf5
BLAKE2b-256 dbaef1f2850e2ad8dfc93693049019532188660098d7e67666971911d51bfe28

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