Skipole WSGI generator.

Topics:

Introduction Getting Started Your Code skiadmin start_call submit_data end_call Exceptions PageData SectionData skicall Serving wsgi Code Examples

Development at GitHub:

github.com/bernie-skipole/skipole

Using Gnuplot to serve an svg image chart

Ensure your server has gnuplot installed.

Create a directory, and move to it, then create a skipole project "myproj", note the trailing space dot, for current directory.

python -m skilift myproj .

which creates the file myproj.py in your current directory.

Edit the start of your myproj.py file to include the import:


import subprocess

Run the project and use skiadmin to create a SubmitIterator responder, with a name such as 'chart.svg', and set with a submit_list string 'gnuplot'. Calling this from the browser will call your submit_data function which should return a binary iterator.

So a submit_data function such as this will do the job:


def submit_data(skicall):
    """This function is called by Responders"""
    if skicall.submit_list[0] == 'gnuplot':
        result = linechart((0, 1), (1, 2), (2, 3))
        pd = PageData()
        pd.mimetype = 'image/svg+xml'
        skicall.update(pd)
        return [result]


def linechart(*dataset):
    "Create an SVG line chart, return svg code as a bytes string"
    # data to plot as a string
    datastring = "\n".join(f"{x} {y}" for x,y in dataset)
    # commands to plot the points
    commands = ['set title "line.svg"',
                'set key off',
                'plot "-" with lines'
               ]
    return svgplot(commands, datastring)


def svgplot(commands, datastring):
    "Call gnuplot with commands, and datastring returns svg bytes"
    commandstring = "set terminal svg;" + ";".join(commands)
    args = ["gnuplot", "-e", commandstring]
    result = subprocess.check_output(args, input=datastring.encode("utf-8"), timeout=2)
    return result

 

When chart.svg is called, this submit_data function returns the svg code and the image is served to the client.