A simple django app to use emojis on your website
Project description
Emoji is a port of the GitHub gem gemoji to Django.
This app got two main use cases:
It’ll try to replace items between :: with emojis, for instance : dog : (without the spaces) will become an emoji of a dog (:dog:).
It’ll try to replace unicode characters with emojis, for instance ‘✌️’ with a victory symbol (:v:).
Quick start
Install django-emoji from PyPi:
pip install django-emoji
Add “emoji” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = ( ... 'emoji', )
Include the emoji URLconf in your project urls.py like this if you want to be able to get a JSON list of emojis:
url(r'^emoji/', include('emoji.urls')),
Visit http://127.0.0.1:8000/emoji/all.json to get a json object with all emojis avilable
Usage
API
Python
The Python class Emoji is a singleton and will return the same instance between instantiations. On load Emoji will load the name of all the emjis and their unicode equivalents into memory.
Call |
Description |
---|---|
Emoji.names() |
A list of all known emojis |
Emoji.replace(replacement_string) |
Replaces all emojies between :: |
Emoji.name_for(character) |
The name for a given unicode character |
Emoji.replace_unicode(replacement_string) |
Replaces all known unicode emojies |
Javascript
The browser version caches all the emojies in localStorage so there won’t be that many roundtrips to the server just to get a list of the emojies.
NOTE: Depends on jQuery or some other library that exports $.get.
Call |
Description |
---|---|
Emoji.setDataUrl(url) |
Where to fetch the list of all available emojis |
Emoji.load() |
Load all emojis from the server |
Emoji.get(/*emoji*/) |
Get the URL to an emoji of a name or return the names of all known emojis |
Emoji.replace(replacementString) |
Replace all :: style emojis with images |
Emoji.clear() |
Empty the browser cache |
Examples
Replace an emoji using Python templates by loading the tags in your template:
{% load emoji_tags %} {{ blog_post.body|emoji_replace }} {{ blog_post.body|emoji_replace_unicode }}
Replace emojis using Javascript (to for instance show a preview before the user saves what it is they are writing):
{% load emoji_tags %} <script src="{% static 'emoji/js/emoji.js' %}"></script> {% emoji_load %} Emoji.get('dog') // => url stub to dog emoji or falsy Emoji.get() // => all emojis available Emoji.replace("It's raining :cats: and :dogs:.") // => It's raining <img src="..." alt="cats" class="emoji"> and <img src="..." alt="dogs" class="emoji">
What emoji_load does is that it sets the endpoint to retrieve the listing of all the available emojis and thus only works if the emoji urls has been included.
It is the equivalent of doing:
Emoji.setDataUrl('{% url 'emoji:list.json' %}').load();
Which is also available as template stub:
{% include 'emoji/script.html' %}
Usage from inside Python where the Emoji class mimics some of the behavior of a dict:
from emoji import Emoji Emoji['dog'] # => url stub to dog emoji or None 'dog' in Emoji # => True Emoji.keys() # => a list of all emojis by name Emoji.replace("It's raining :cats: and :dogs:") # => It's raining <img src="..." alt="cats" class="emoji"> and <img src="..." alt="dogs" class="emoji">
Replacing unicode Emojis
Emoji has the ability to give you the name of an emoji from a unicode character. It can also replace every instance of emoji characters in a string with their image replacements.
Usage:
>>> from emoji import Emoji >>> Emoji.name_for(u'\U000148b') kiss >>> Emoji.replace_unicode(u'I send a \U0001f48b!') I send a <img src="/static/emoji/img/kiss.png" alt="kiss" class="emoji">
Note:
To be able to use the unicode replacements your Python version needs to be built with wide unicode character support. This seems to be the case for most Pythons available from package managers. If you do not have wide character support then an exception will be raised:
>>> print(unichr(0x0001f48b)) ValueError: unichr() arg not in range(0x10000) (narrow Python build)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-emoji-1.1.0.tar.gz
.
File metadata
- Download URL: django-emoji-1.1.0.tar.gz
- Upload date:
- Size: 4.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0aec0477a4e3be6eec88a508d2f045d1b8ca831ff6746d953744d38ef982d41 |
|
MD5 | e5fe745fa599fb8af0808097abe32624 |
|
BLAKE2b-256 | bf26ee8d3383d0daeee98b9ab914a6972c4793847b99734180915bb2b650a01c |