Skip to content

Instantly share code, notes, and snippets.

@krets
Last active October 6, 2019 22:19
Show Gist options
  • Save krets/e79acaf1368adbeec7db8630d3d8ccd9 to your computer and use it in GitHub Desktop.
Save krets/e79acaf1368adbeec7db8630d3d8ccd9 to your computer and use it in GitHub Desktop.
""" Test for drawing base64 image strings for job progress
Example Job:
{
'comment': '',
'maxslots': 0,
'numblocked': 0,
'spoolcwd': '/home/freddie/unitard,
'numerror': 0,
'aftertime': None,
'elapsedsecs': 0.0,
'owner': 'paul',
'spoolhost': 'zanzibar,
'spooladdr': '138.72.196.92',
'jid': 2680,
'service': '',
'title': 'Somebody To Render',
'numactive': 0,
'editpolicy': '',
'spooltime': '2014-02-17 08:16:53-08',
'projects': [
'flash_gordon',
'hot_space'],
'crews': [
'queen'],
'maxcid': 2,
'metadata': '',
'numready': 0,
'tags': [],
'afterjids': [],
'stoptime': '2014-02-17 08:16:57.971-08',
'envkey': [],
'tier': '',
'etalevel': 1,
'numtasks': 2,
'numdone': 2,
'maxtid': 2,
'assignments': '',
'spoolfile': '/home/freddie/champion.alf',
'esttotalsecs': 0.0,
'starttime': '2014-02-17 08:16:53.73-08',
'pausetime': None,
'minslots': 0,
'deletetime': None,
'priority': 10.0
}
"""
from collections import OrderedDict
from PIL import Image
import random
import base64
import cStringIO
COLORS = OrderedDict([
('numdone', (107,129,140)),
('numerror', (204,16,31)),
('numactive', (79,224,60)),
('numblocked', (242,204,79)),
('numready', (3,30,43)),
])
def job_progress(job):
""" Tractor job object
Args:
job (dict): with the following keys:
numblocked
numerror
numactive
numready
numtasks
numdone
Returns:
base64 image data
"""
width = 100
img = Image.new('RGB', (width, 1))
pixels = img.load()
total = sum([job.get(_) for _ in COLORS])
x = 0
for attr, color in COLORS.items():
val = job.get(attr)
pct = int(float(val)/total * width)
if val > 0:
pct = max(1, pct)
for _ in range(pct):
pixels[x, 0] = color
x+=1
handle = cStringIO.StringIO()
img.save(handle, format="PNG")
return base64.b64encode(handle.getvalue())
def main():
jobs = []
for i in range(50):
job = {}
for attr in COLORS:
job[attr] = random.randint(0,256)
jobs.append(job)
with open('tractor_progress_bars.html', 'w') as fh:
for job in jobs:
fh.write("""
<img src="data:image/png;base64, %s" style='height:1em;width:100px;margin:2px;'/>
<br/>
""" % job_progress(job))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment