How to upload or distribute matplotlib stylesheet
I want to distribute a custom matplotlib stylesheet , but for now I can only think of uploading it to a Gist or other website and asking my users to manually upload it to some config directory.
Is there a way to distribute the stylesheet as if it were a Python package or as part of a module? Something simple like pip install mpl_fancy
.
source to share
After reading the link to @aloctavodia and consulting the massmutual / mmviz-python GitHub repository , I came to this.
setup.py
from setuptools import setup
from setuptools.command.install import install
import os
import shutil
import atexit
import matplotlib
def install_mplstyle():
stylefile = "mystyle.mplstyle"
mpl_stylelib_dir = os.path.join(matplotlib.get_configdir() ,"stylelib")
if not os.path.exists(mpl_stylelib_dir):
os.makedirs(mpl_stylelib_dir)
print("Installing style into", mpl_stylelib_dir)
shutil.copy(
os.path.join(os.path.dirname(__file__), stylefile),
os.path.join(mpl_stylelib_dir, stylefile))
class PostInstallMoveFile(install):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
atexit.register(install_mplstyle)
setup(
name='my-style',
version='0.1.0',
py_modules=['my_style'],
install_requires=[
'matplotlib',
],
cmdclass={
'install': PostInstallMoveFile,
}
)
In my_style.py
I just put a basic example script. Now my users can set this style with pip install git+https://github.com/me/my-style
!
source to share