Emojis Not Working

Emojis doesn’t seems to work, how can I do it? Tried different fonts also.

from graphviz import Digraph

def create_family_tree(husbands, wives, children):
    family_tree = Digraph('FamilyTree', format='png', node_attr={'shape': 'record'})

    for husband, wife in zip(husbands, wives):
        family_tree.node(husband, label=f"{husband} | {wife}")

    for parent, kids in children.items():
        for child in kids:
            family_tree.node(child)
            family_tree.edge(parent, child)

    family_tree.render(filename='family_tree', format='png', view=True)

husbands = ["Malik✅"]
wives = ["Mehwish"]
children = {"Malik✅": ['Sultan', "Usman"]}

family_tree = create_family_tree(husbands, wives, children)

Works for me on macOS. Here’s it in a code block so the forum doesn’t change the quotes to smart quotes:

from graphviz import Digraph

def create_family_tree(husbands, wives, children):
    family_tree = Digraph('FamilyTree', format='png', node_attr={'shape': 'record'})

    for husband, wife in zip(husbands, wives):
        family_tree.node(husband, label=f"{husband} | {wife}")

    for parent, kids in children.items():
        for child in kids:
            family_tree.node(child)
            family_tree.edge(parent, child)

    family_tree.render(filename='family_tree', format='png', view=True)

husbands = ["Malik✅"]
wives = ["Mehwish"]
children = {"Malik✅": ['Sultan', "Usman"]}

family_tree = create_family_tree(husbands, wives, children)
$ python3.9 a.py

image

What OS are you on? What editor are you using? Are you saving the source as UTF-8?

perhaps most importantly, what do you mean “doesn’t seems to work” ? What do you observe?

I have tried on Windows 11 Python 3.10 and Linux Python 3.10, and got same results,
family_tree

It appears the Times-Roman font found by fontconfig does not have emojis. You could verify this by fc-match -v Times-Roman to find the file, and use some other program to view it. There is also a simple command line tool (somewhere) to render UTF-8 via pango but I don’t remember what it is any more; you’d have to search for it. I think you can make a personal fonts.conf file to control fontconfig bindings.

It might be easier to set the graph, node, and edge fontname in the graphviz file to a different font that has emojis. I’m not sure what font that would be. (Obviously you already have one somewhere.)

There’s no way to globally change the default graph, node, and edge label fonts in Graphviz with one setting, unfortunately. This would be a nice minor enhancement.

Sometimes we have characters that doesn’t work with a single font, as encoding is utf-8 by default, it was better to handle this stuff automatically by library.

import os
import unicodedata
from fontTools.ttLib import TTFont

def get_windows_fonts():
    windows_fonts = []
    windows_font_directories = [
        os.path.join(os.environ.get('SystemRoot', 'C:\\Windows'), 'Fonts')
    ]
    for font_dir in windows_font_directories:
        if os.path.exists(font_dir):
            for root, dirs, files in os.walk(font_dir):
                for file in files:
                    if file.lower().endswith(".ttf"):
                        windows_fonts.append(os.path.join(root, file))
    return windows_fonts

def char_in_font(unicode_char, font):
    try:
        for cmap in font['cmap'].tables:
            if cmap.isUnicode():
                if ord(unicode_char) in cmap.cmap:
                    return True
    except AssertionError as e:
        pass
    return False


def test(char):
    windows_fonts = get_windows_fonts()
    for font_path in windows_fonts:
        font = TTFont(font_path)
        if char_in_font(char, font):
            print(f"{char} {unicodedata.name(char)} in {font_path}")


characters = '@ঔৣ☬✞𝔚𝔥𝔞𝔱𝔢𝔳𝔢𝔯✞☬ঔৣ'
for char in characters:
    test(char)