Skip to content

Instantly share code, notes, and snippets.

@hxy9243
Created July 27, 2015 00:00
Show Gist options
  • Save hxy9243/212177806830d0ec09a0 to your computer and use it in GitHub Desktop.
Save hxy9243/212177806830d0ec09a0 to your computer and use it in GitHub Desktop.
CGI script
#!/usr/bin/env python
import cgi
import json
import time
import random
print("Content-type:text/json\n")
form = cgi.FieldStorage()
user = form.getfirst("username", "")
password = form.getfirst("password", "")
if user == None:
user = "Sir"
data = [
['Date', 'Geekbench', 'v8'],
['07/10', 1020, 241],
['07/11', 1023, 231],
['07/12', 1032, 256],
['07/13', 1025, 261],
['07/14', 1067, 234]]
for i in data:
if i[0] == 'Date':
pass
else:
i[1] = 1000 * random.random()
i[2] = 1000 * random.random()
print(json.dumps(data))
#!/usr/bin/env python
import cgi
print("Content-type:text/html\n")
f = open("main.html", "r")
h = f.read()
f.close()
print(h)
<html>
<head>
<title>Hello World</title>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={
'modules':[{
'name':'visualization',
'version':'1',
'packages':['corechart']
}]}"></script>
<script type="text/javascript" src="/res/script.js"></script>
</head>
<body>
<h1>Hello World</h1>
<p>How are you doing?</p>
<p><form id="param-form">
<input type="username" name="username" id="username"/>
<input type="password" name="password" id="password"/>
<button type="button" id="submit-button" disabled="disabled">Submit</button>
</form></p>
<div id="div_chart"></div>
</body>
</html>
google.setOnLoadCallback(loadButton);
function loadButton(){
// Send XMLHttpRequest
var submit_button = document.getElementById("submit-button");
submit_button.removeAttribute("disabled", "false");
submit_button.onclick = function(){
var resp = XHR(drawChart);
};
}
// function to send XHR to cgi
function XHR(callback){
var formElement = document.getElementById("param-form");
var formData = new FormData(formElement);
var dataRequest = new XMLHttpRequest();
var resp;
dataRequest.open("POST", "/data.cgi", true);
dataRequest.send(formData);
dataRequest.onload = function() {
// console.log(dataRequest.responseText);
resp = JSON.parse(dataRequest.responseText);
callback(resp);
}
}
// Draw chart
function drawChart(input_data){
// console.log(input_data);
var data = google.visualization.arrayToDataTable(input_data);
var options = {
title: 'Company',
curveType: 'function',
legend: {position: 'bottom'}
};
var chart = new google.visualization.LineChart(
document.getElementById('div_chart'));
chart.draw(data, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment