Skip to main content

Python Web served time graph

Project description

webtimeline

Python Web served time graph

This provides a class 'WebTimeLine' which generates a web server, serving a page with a lineplot.

A coroutine method of the class can be used to add points, in the form of (time.time(), value)

These are dynamically added to the plot, and the web page will be updated as points are added.

An example browser image is:

Terminal screenshot

The program can be installed from Pypi into a virtual environment, which will automaticall pull in dependencies.

Running "python3 -m webtimeline" will then run the following script and will serve the above chart:

import asyncio, time, random

from webtimeline import WebTimeLine

## This creates an example web service, with random measurements every ten seconds

tline =  WebTimeLine(host='localhost', port=8000, basepath=None, hours=1, title="My Title", description="Data display")

# set a y axis, lowest value 0.0
#               highest 80.0
#               with four intervals up the axis (five values shown at grid lines)
#               and axis numbers printed with one decimal point

tline.set_y_axis(ymin=0.0, ymax=80.0, yintervals=4, yformat=".1f")

async def my_function(tline):
    "Create data and send it using tline.putpoint()"
    while True:
        value = random.uniform(30, 70)   # random value used here
        tline.set_description(f"Latest value = {value:.1f}")
        await tline.putpoint(time.time(), value)
        await asyncio.sleep(10) # pause 10 seconds between readings

## create two tasks, one runs the web server, one gathers data

async def runchart():
    async with asyncio.TaskGroup() as tg:
        tg.create_task( tline.serve(tg) )
        tg.create_task( my_function(tline) )
        print("Now serving at localhost:8000")

asyncio.run(runchart())

Alternatively, if you use UV, simply running "uvx webtimeline" will load and run the above.

The web page is purposely minimal, without any CSS. The code could be obtained from github and the Mako templates altered for a more pleasant view. Feel free to adapt, this is public domain using the 'unlicense'.

Dependencies are:

litestar[standard]
mako
minilineplot

Details of the WebTimeLine class are:

WebTimeLine(host, port, basepath, hours, height, width, title, description)

host is a string, default 'localhost'

port is an integer, default 8000

basepath is either None, or a string such as '/graph/' which will set a path segment which will be prepended to the URL path.

hours is the hours, between 1 and 48, displayed along the x axis.

height is the height of the image, default 600.

width is the width of the image, default 800.

title, if given is a string shown at the top of the graph.

description, if given, is a string shown at the bottom of the graph.

Methods

serve(tg)

(async method) Set this as a task to serve the web page, tg should be a taskgroup

putpoint(t, v)

(async method) await this to add a point to the graph.

t must be a time.time() point, that is, a float which is seconds since January 1st 1970.

v is the value to be plotted.

The following methods, if called, will take affect on the next chart update when putpoint is called.

set_colors(backcol, gridcol, axiscol, chartbackcol, linecol)

If called sets chart colours.

backcol is default "white", The background colour of the whole image

gridcol is default "grey", The colour of the chart grid

axiscol is default "black", The colour of axis, title and description

chartbackcol is default "white", The background colour of the chart

linecol is default "blue", The colour of the line being plotted

All these colour names are SVG names and can be set as:

Color Names: "red", "blue" etc.

Hex Codes: "#FF0000" for red.

RGB/RGBA: "rgb(255,0,0)" or "rgba(255,0,0,0.5)" (with opacity).

HSL/HSLA: "hsl(0,100%,50%)" or "hsla(0,100%,50%,0.5)" (hue, saturation, lightness, alpha)

set_title(title)

Sets a new title on the graph.

set_description(description)

Sets a new description at the bottom of the graph.

set_y_axis(ymin, ymax, yintervals, yformat)

Sets the y axis minimum and maximum values.

If this is not called, an automatic y scaling will be used.

If it is called, then these values will be set, however if any y point exceeds these values, then the chart will revert to auto-scaling.

If you wish to purposely revert to auto-scaling, call this with None values.

yintervals sets the grid and number intervals up the y axis.

yformat is a string which defines how the y axis numbers are displayed. So the string ".2f" will show numbers with two decimal places.

The auto-scaling feature will inspect the line points, and attempt to set all these values automatically.

set_localtime(tflag)

If tflag is True, (the default) the time values on the x axis will show local time (local time of the server).

If tflag is False, the time values will be UTC.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webtimeline-0.0.2.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webtimeline-0.0.2-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file webtimeline-0.0.2.tar.gz.

File metadata

  • Download URL: webtimeline-0.0.2.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for webtimeline-0.0.2.tar.gz
Algorithm Hash digest
SHA256 69788c3a739965990c269535e6058bc3a6bd130d7e5408c7302227c5ae018832
MD5 d81a150ea541575a6951ee24d8b2e97d
BLAKE2b-256 9d6bed8a59003a282d470c25280617e3a28fb9153efc296d05fbdc55c22561c6

See more details on using hashes here.

File details

Details for the file webtimeline-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: webtimeline-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for webtimeline-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d04254998345b4f6e05577736a0520b2f5e25641d692b98fa788cdb1c5294c4a
MD5 048dc1006c63ae7f743481f4be2e027f
BLAKE2b-256 15c4cbe67fff5fd2b1795243baabbd41c1018d2275e4babaa17a32fcd94f0abe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page