Search code examples
pythonread-the-docs

Is it possible to generate images and .rst files on build with read-the-docs?


I have a Python package containing a data module with some data in .json format (https://github.com/oemof/tespy/tree/dev/tespy/data). I want to document these data as plots in my online documentation like this (https://tespy.readthedocs.io/en/dev/api/tespy.data.html).

Is it possible implement a python script into the readthedocs build and document the data automatically this way? I think, this would be useful, since changes in the data will automatically be documented. Or, would this be bad practice?

At the moment, I have python script creating the .rst file (tespy.data.rst) as well as the plots (.svg format) locally and upload them to the github repository. My code requires matplotlib, pkg_resources as well as json and looks like this (is the pseudo-code okay, or should I add the full code?).

import json
from matplotlib import pyplot as plt
from pkg_resources import resource_filename

def get_data():
    path = resource_filename('tespy.data', 'char_lines.json')

    with open(path) as f:
        data = json.loads(f.read())

    return data

def plot_line(data):
    fig = plt.figure()
    [plotting_code]
    fig.savefig(path + '.svg')

def generate_rst(data):
    [rst code generation here]
    return rst_code

for key, data in get_data().items():
    [data_handling_code_here]
    plot_line(data)
    rst += generate_rst(data)

Solution

  • I was able to solve this: I integrated the plotting code into the conf.py. Additionally I added the readthedocs requirement for matplotlib. See: https://github.com/oemof/tespy/blob/dev/doc/conf.py and https://github.com/oemof/tespy/blob/dev/rtd_requirements.