Send data from Python to Javascript via network connection - javascript

This is more a general question, I'm not asking for implementation details.
I have a Python script running on a machine which monitors an experiment and gathers live data from it. I want to visualise the data on a website and decided to go with d3.js
My current setup however is absolutely basic:
The python script is running in an endless loop and appends new data
to a text-file (CSV file).
There is an HTML-page on the web
server with a javascript, which grabs the file and creates a graph
using d3.js.
I already thought about replacing the text-file with a sqlite database, but I'm sure there is a more sophisticated and easy way to do it via network.
So I am looking for a more solid solution and searched the web for different kind of strategies but most of those involve bigger libraries (Tornado, node.js etc.).
I would be happy with quite basic socket-like solution and tried to figure out how it would work with WebSockets but not sure how to handle this.
So my question is, what kind of design would you recommend or use if your only goal is to have a Javascript running in a browser and waiting/listening for data (let's say every few seconds a float), which is sent from a Python script?

Related

Bridging a Python back-end and JavaScript front-end

I'm currently working on a project that involves parsing through a user-supplied file, doing computations with that data, and visualizing the results using graphing utilities. Right now, I'm stuck with using Python as the back-end because it has scientific libraries unavailable in JavaScript, but I want to move the entire tool to a web server, where I can do much slicker visualizations using D3.js.
The workflow would be something like: obtain the file contents from the browser, execute the Python script with the contents, return jsonified objects of computed values, and plot those objects using D3. I already have the back-end and front-end working by themselves, but want to know: How can I go about bridging the two? From what I've gathered, I need to do something along the lines of launching a server, sending AJAX requests to the server, and retrieving data from the server. But with the number of frameworks out there (Flask, cgi, apache, websockets, etc.), I'm not really sure where to start. This will likely only be a very simple web app with just a file submit page and a data visualization page. Any help is appreciated!
Apache is a web server, flask is a web framework in python, websockets are a protocol, and cgi is something totally different, and none of them help you on the front end.
You could deploy a simple backend in flask or django or pylons or any other python web framework. I like django, but it may be a little heavy for your purpose, flask is a bit more lightweight. You deploy them to a server with a web server installed and use something like apache to distribute.
Then you need a front end and a way of delivering your front end. Flask / Django are both fully capable of doing so in conjunction with a web server, or you could use a static file server like Amazon S3.
On your front end, you need to load D3 and probably some kind of utility like jQuery to load and parse your data from the back end, then use D3 however you like to present it on screen.
Flask is easy to get up and running and is Python based. It works well with REST APIs and data sent by JSON (or JSON API).
This is one solution with which I have some experience and which seems to work well and is not hard to get up and running (and natural to work with Python). I can't tell you whether it is the best solution for your needs, but it should work.
If you are overwhelmed and don't know where to start, you can pick one of the options and google search for a tutorial. With a decent tutorial, you should have an example up and running by the end of the tutorial, and then you will know if you are comfortable working with it and have an idea whether it will meet your needs.
Then you could do a proof-of-concept; make a small app that just handles one small part (the one you are most concerned about handling, perhaps) and write something which will do it.
By then, you can be pretty sure you have a good tool for the purpose (unless you were convinced otherwise by the proof-of-concept -- in which case, try again with another option :-))

Web site/app - Real-Time Charting Advice (mySQL, JS, PHP?)

I'm working on a project to create real-time charts from data in an SQL database. I've done a lot of research on JS charting libraries and have picked out a few good ones.
My problem is that I'm not sure how to get started with linking the SQL data to the web app/site.
My HTML and CSS are fine. I'm coming on with JS and we have software that provides me with necessary SQL query code.
Where is the gap in my knowledge? Is it necessary to learn PHP in order to get this working? How about C#?
My solution will currently be a locally hosted site, running on a networked computer (not currently got server space). The solution I have already uses Excel to query the database and runs a ton of macros to generate static charts every ten minutes. This works, but I'd like to create something far more flexible, robust and interactive.
Could anybody point me to any online resources that may help me? I like ChartJS but any open-source charting library will do. I'm hoping there's a great tutorial somewhere that covers SQL to real-time charts but I just can't find one...
If you want a real time charting you need to receive information from the DB almost every second. So sending a bunch of requests is not the best way to do this. You probably need to read about websockets and how do they work. Here is an example but you may find a lot more in google. If you don't want to do it with PHP, I can assure you that it would be far more easier with node.js

JS: Fastest way to push console output to client

I am currently creating a real time online web console that takes output from Bash and displays it on a PHP driven, HTML page. At the current moment, I'm using the method of fetching the entire contents of a page and replacing a DIV's contents with the contents of the fetched page, which is very inefficient, and am wondering how to do this more efficiently, such as with WebSockets, Faye, or long polling.
What is the fastest way I can get Bash output to a client?
If you think I should use something that requires a server-side install, why?
WebSockets will give you significantly lower latency than AJAX/long-polling requests if that is important for your project.
There is a telnet example included with websockify (Disclaimer: I made websockify). Websockify bridges between websockets and TCP sockets. You might be able to use some ideas from that. In particular, you may want to look at the Javascript terminal emulator code that is included with that example if you want to implement anything more than just display of raw text.

is json the answer to this: python program will talk and javascript will listen?

the same problem haunting me a month ago is still haunting me now. i know ive asked several questions regarding this on this site and i am truly sorry for that. your suggestions have all been excellent but the answer is still elusive. i now realize that this is a direct result of me not being able to phrase my question properly and for that i am sorry.
to give you guys a generalized view of things, here i go: the situation is like this, i have 2 server side scripts that i want to run.
a python program/script that continuously spouts some numbers
based on the output from that python script, a javascript script will perform some action on a webpage (e.g., change background color, display alert message, change some text)
ive studied the replies to my previous posts and have found that what i want to accomplish is more or less accomplished by json. it is my understanding that json transforms 'program-specific' variables into a format that is more 'standard or general or global'.
two different programs therefore now have the means to 'talk' with each other because they are now speaking the same 'language'.
the problem is then this, how do i actually facilitate their communication? what is the 'cellphone' between these server side scripts? do they even need one?
thank you!
If I understand what you're asking, the "cellphone" is TCP/IP. The javascript is not server-side; it runs on the client side, and alters what the client's browser displays based on json data that it downloads from the server -- data that in this case is generated by Python.
This question provides a relevant example, though it's a bit technical: JSON datetime between Python and JavaScript
Here's a very basic tutorial that explains how to create a dynamic webpage using python and javascript. It doesn't appear to use json, but it should familiarize you with the fundamentals. Once you understand what's there, using json to transport more complicated data should be fairly straightforward.
http://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/
I assume you mean: Python is on the web server, and Javascript is running in the client's web browser.
Because browsers are all different (IE6 is terrible, Chrome is great), there are a huge number of ways people found to "hack" this "cellphone" into place. These techniques are called AJAX and COMET techniques. There is no one "cellphone", but a whole bunch of them! Hopefully, you can find a library to select the right technique for the browser, and you just have to worry about the messages.
Comet is harder to do, but lets the server "push" messages to the client.
Ajax can be easier - you just periodically "pull" messages from the server.
Start with Ajax, then look at comet if you really need it. Just start by have the client (javascript) make a "GET" request, to see if the number has changed.
I don't know Javascript or json, but...
if you've ever seen an Unix-like operating system, you know about pipes. Like program1 | program2 | program3 ... Why don't you just connect Python and Javascript programs with pipes? The first one writes to stdout, and the next one reads from stdin.
This probably isn't the answer that you are looking for, and without links to your previous posts, I don't have much to go on, but nonetheless...
javascript is client side. I can interpret your question 2 different ways...
Your python script is running on your computer, and you want a script to actually alter your current browser window.
Not too sure, but writing a browser plugin may be the answer here.
Your python script is running on the server, and as a result of the script running, you want the display of your site to be changed for viewing persons.
In this case, you will could use ajax polling (or similar) on your site. Have your site be polling the server with ajax, call a server method that checks the output of the script (maybe written to a file?), and see if it has changed.
When 2 process need to communicate, they need to decide of a common/shared way to express things and a protocol to exchange those things.
In your case, since one of the processes is a browser, the protocol of choice is http. So the browser needs to do an http request or regular http request to your python process.
This python process Will need in Some way or another to be exposed via http.
There are several ways to build a web server in python. You should read this article : http://fragments.turtlemeat.com/pythonwebserver.php as a jumpstart.
Once you have this, your browser Will be able to issue HTTP GET requests to your server and your server can reply with a string.
This string can be whatever you like. Nevertheless if your answer contains structured data it can be a good start to use the XML notation or the json notation.
Json (stands for Javascript object notation) is very easy to use in javascript and this is why many people advised you to choose this notation.
I hope this will help you
Jérome wagner

Google Gadget, Javascript (Or Other) Way To Port Collected Data

I am working on a Google Gadget that will collect some data through Google API's. What I am getting stuck on is how to collect the data and then save it somewhere to be processed later. The final idea being that I would run the gadget on my own computer it would collect the data and then save it to somewhere on my own computer. (I guess I want to emphasize that this is, for now, a small personal project and does not necessarily need fancy server scripts, I want to be able to run this all on my PC running XP).
Is there a pure Javascript way to save a file on a computer?
Can I use other languages besides XML, HTML, and Javascript to add functionality to my Google Gadget?
Edit: The goal of this is to be able to log how many of my contacts are signed into gchat over a period of time. I decided on a Gadget because that was the only way I could figure out how to access that information. Any other ways to approach this idea are welcome!
No, Javascript alone cannot save a file automatically. And be careful, javascript is affected by the no cross domain rule. If you're hosting the project on your own computer, why bother writing a complex Google Gadget?
I suggest a simple PHP script, and MySQL, if you like, to store the data. By itself, PHP should be more than enough to run most tasks. If you would like me to add in more info about this, please tell me what type of task.
In increasing order of flexibility:
The options object is almost certainly the easiest approach - not really designed for that kind of usage but I suspect it would be fine for your use case.
On windows you could use system.filesystem to get hold of the WScript FileSystemObject which you can then use to create files and write to them.
Also see the Google desktop API blog for embedding an SQLite database in your gadget (looks pretty easy).

Categories