Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to send updated json data from microwebserver to json file that embedded in html file #43

Closed
m2030 opened this issue Sep 7, 2018 · 2 comments

Comments

@m2030
Copy link

m2030 commented Sep 7, 2018

great project it is easy and simple
how to send updated json data from microwebserver to json file that embedded in html file with jquery ajax

`

<script type="text/javascript"> window.onload = function() { var dataPoints = []; var chart; $.getJSON("/livegraph2.json", function(data) { x=0;
	$.each(data, function(key, value){
		dataPoints.push({x:x},{y: parseInt(value)});
		x++;
		xaxis=x;
	});
	chart = new CanvasJS.Chart("chartContainer",{
		title:{
			text:"Live graph for spo2 pleth"
		},

axisX:{
title: "time in microsecond"//,
// minimum: xaxis , // change here
// maximum: xaxis+100
},
axisY:{
title: "in melevolt"//,
// minimum: xaxis , // change here
// maximum: xaxis+100
},
data: [{
type: "line",
dataPoints : dataPoints,
}]
});
chart.render();
updateChart();
});
var dataLength = 20;
function updateChart() {
$.getJSON("/livegraph2.json", function(data) {
$.each(data, function(key, value) {
dataPoints.push({
x:x,
y: parseInt(value)
})
x++;
if (dataPoints.length > dataLength) {
dataPoints.shift();
}
});

	chart.render();

	setTimeout(function(){updateChart()}, 1000);
});
}

}
</script>

<script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
` like with micropython code ` import gc import os gc.collect() from microWebSrv import MicroWebSrv array=[80,90,86,89,90,89,86,95,94,91,89,97,94,100,110] array1='[' def _httpHandlerarray(httpClient, httpResponse): gc.collect() for x in array: array1=str(x+7) array1=',' array1[-1]=']' ## f=open('/www/livegraph2.json','w') ## f.write(mystring) ##f.close() ##print(mystring) httpResponse.WriteResponseOk(headers=None, contentType= "application/json", contentCharset="UTF-8", content=array1)

routeHandlers=[("/livegraph2.html","GET",_httpHandlerarray)]
srv=MicroWebSrv(routeHandlers=routeHandlers,webPath='/www/')
gc.collect()
print(array1)
srv.Start(threaded=False)`

@jczic
Copy link
Owner

jczic commented Sep 17, 2018

Hello :)
Sorry for latence !

You can just respond in JSON format from a handler function like that (check main.py) :

    httpResponse.WriteResponseJSONOk({
        'example1'    : 12345,
        'example2'    : True,
        'example3'    : 'toto'
    })

@m2030
Copy link
Author

m2030 commented Sep 18, 2018

thanks jczic

@m2030 m2030 closed this as completed Sep 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants