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


Release history Release notifications

This version
History Node

0.1.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
Flask_Avatars-0.1.0-py2.py3-none-any.whl (281.7 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Jun 19, 2018
Flask-Avatars-0.1.0.tar.gz (262.6 kB) Copy SHA256 hash SHA256 Source None Jun 19, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page