Skip to content

Instantly share code, notes, and snippets.

@nitaku
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitaku/ac740047375294b075f5 to your computer and use it in GitHub Desktop.
Save nitaku/ac740047375294b075f5 to your computer and use it in GitHub Desktop.
Python template

A simple template for a WebVis gist that enables the execution of python code. In this example, a random color is chosen with python's random.choice() method, a new PNG image filled with the chosen color is created with the Python Imaging Library, and finally the image is displayed in a wrapper web page.

Strings that are printed by the python code are also sent to the JavaScript console (try to open the console to see that in action).

<?php
$LOG = json_encode(shell_exec('python main.py'));
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Python template</title>
<style>
html, body { padding: 0; margin: 0; }
</style>
</head>
<body>
<img src="output.png"></img>
<script>
var __log = <?= $LOG; ?>;
if (__log)
console.log(__log);
</script>
</body>
</html>
from PIL import Image
from random import choice
color = choice(('red','green','blue'))
print 'A random color has been chosen: %s' % color
im = Image.new('RGB', (960, 500), color)
im.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment