'Custom ttf font with python plotly on headless linux machine

I want to create plotly diagrams in different languages and fonts and save them as svgs in python. As the application will be installed on docker systems which does not have GNOME and user would select the language and font for their diagrams.

As per the plotly documentation, it supports system fonts that are installed to be used for change the font by updating font property in layout. For example: font=dict(family="Franklin Gothic", size=18)

My requirement is that as I have linux system without GUI, I cannot install ttf fonts. And the python script should run on Windows, Mac and Linux (for servers and docker systems without GNOME) to generate plotly diagrams and save SVG with the custom font local ttf files which are stored in the local directory.

import plotly.graph_objects as go
import numpy as np


t = np.linspace(0, 10, 100)

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=t, y=np.sin(t),
    name='sin',
    mode='markers',
    marker_color='rgba(152, 0, 0, .8)'
))

fig.update_layout(title='Styled Scatter',
                  yaxis_zeroline=False, xaxis_zeroline=False)

fig.show()

I want something like below to work: fig.update_layout(font=dict(family="/usr/fonts/Noto Sans.ttf", size=18)

Currently, this is not considered by plotly and it seems to be using default font that are installed with plotly installation.

It shall be of great help.

Thanks,



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source