My question is really simpel, but I haven't found it yet.
I have a python script running on my server. This script reads NFC cards. (pyscard)
Everytime when there is a card the cardID must send to a webpage. I have in index.html file on my localhost.
<head>
<title>Hello!</title>
</head>
<script language="JavaScript" type="text/javascript">
function card(Cardid){
alert(Cardid);
}
</script>
<body>
</body>
</html>
There is a way to do it by selenium (driver.execute_script(command) ) Selenium is to heavy. Is there a other simple and light way to do this?
If I got you right, you need to implement AJAX. Here is simpliest example How to implement a minimal server for AJAX in Python?
If you'll need something more complex — chose one of the python web frameworks (Flask or Django) and look for tutorials how to implement AJAX with them.
If you want to push cardId to webpage you need tot use something in between like socket.IO then on a listening channel pushing the data to all websocket clients. From there you can pass it to a function like yours.
The other way to do it is a bit more limiting.
You can make Ajax call to python which reads current card holding data and use that id. But when another card is on the reader,you then have tot refresh to see the new cards data.
Related
I'm working on text game project in Python. Currently i have finished console app + sqlite database. Now I want to convert console app to web app - it will be the first web app in my life.
I want to create a simple GUI. With main logo, background image, several buttons and text zones. Example of simple GUI project:
simple gui project
I would like the logic of the application to be based on the code already created for console application. For example, by replacing the current console functions (for example print) with a function that returns data in the form of JSON. But without changing the internal logic of the function already written in Python. Is it possible?
What is the easiest way (and what technologies?) to do that?
converting a python application to a web application is not a very practical task in some cases.
I think you should use something like Flask or Django but if you don't want to complicate your life too much, there may be an alternative, and it's called PyPy.js
The first things that you must take into account in the logic of web applications, is that HTML is the skeleton of what you are going to show, so you must study it, Javascript adds dynamism to your page, such as animations and data updates without reloading the page, and the CSS to add styles to your html tags.
<head>
<script src="http://pypyjs.org/pypyjs-release/lib/Promise.min.js"></script>
<script src="http://pypyjs.org/pypyjs-release/lib/FunctionPromise.js"></script>
<script src="http://pypyjs.org/pypyjs-release/lib/pypyjs.js"></script>
</head>
<body>
<script type="text/javascript">
pypyjs.exec(
// Run Python code
'y = "hellow" '
).then(function() {
// transferring the variables we need to Javascript, in this case only 'y'.
return pypyjs.get('y');
}).then(function(result) {
// Display an alert like print in Python
alert(result);
});
</script>
</body>
that would be the same as ...
y = "hellow"
print (y)
AM trying to implement a push server for my pHP application.
My initial thoughts were as long as i keep the EVENTS called on my client let say message_view.php file . I would not have a problem of emiting node js events.
But i see that most of the tutorial online use something like I dont understood this ?
fs.readFile(__dirname + '/client.html') ...//html files
or
response.sendfile('hello world')
After they have started the server. Then they add event and logic as follow on the client html file that am supposed to do it on my messages_view.php file.
<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
and socket emiting events like this one :
<script>
// create a new websocket
var socket = io.connect('http://localhost:8000/?profile_id=abcde2');
//..I want to code here to update my divs.
</script>
What do i need exactly to emit this message on my PHP file. Many Thanks!
If you want to serve html with php, and have a node.js socket server, the socket server doesn't need to serve html, so you can omit the majority of the example you're using. Just include the script in your .php that serves the html page and make sure the url to the script properly targets the socket server.
i have a c++ file which reads values from a sensor and I want to display those values on a website dynamically. So Im looking for a way to pass these values(integers) from my cpp file to an javascript which displays them on the site.
My first, simple try was to write the values into a js file as variables every second from my cpp script. The Js then uses this file as a source and displays its variables on the site:
cpp:
fprintf(file, "var mx=%d, my=%d, mz=%d, ax=%d, ay=%d, az=%d, gx=%d, gy=%d, gz=%d;\n",
imu.raw_m[0], imu.raw_m[1], imu.raw_m[2], // M = Magnetometer
imu.raw_a[0], imu.raw_a[1], imu.raw_a[2], // A = Accelerometer
imu.raw_g[0], imu.raw_g[1], imu.raw_g[2] // G = Gyroscope
);
html/js:
<script src="./imu.js" type="text/javascript"></script>
The Problem now is of course, that I need to refresh the page all the time, because the imu.js file is cached by the website.
I'd rather have a way to directly pass to integers from the cpp file to the js script. I read something about json or Googles V8 script. But I'd like to hear your suggestions first.
By the way, Im running this on a raspi, if this is important.
Thanks for your help
EDIT:
I'm goning to try it with a mysql database, in which my cpp file writes the data from the sensor with Connector/c++ from http://dev.mysql.com/doc/connector-cpp/en/ and my website reads them.
You could compile your C++ code into a Node.js plugin, you can then register a JavaScript function with your plugin which the C++ calls when it updates the value. That way you can pass values directly from C++ into Javascript in a managed and controlled way.
Node.js has the added benefit of being able to host your webpage and do all the Websocket and HTTP stuff that can be a pain in C++.
You do not have to refresh if your script is smart about how to access the data file! In case you do have a webserver at hand: Take care that your data file is accessible by your webserver and then let your script request the file via ajax (link to w3schools)
I'm doing something similar on a BeagleBone Black. With websocketd you can turn pretty much any program into a websocket endpoint and then send data via stdin and stdout commands. This would be a particularly good solution for you since websockets are designed to handle information that's constantly changing.
Opening a site economizing the data load I seem to run into a need to add more data from Python occasionally. I use Python with CherryPy and Mako for loading a site.
So, how can I make a JavaScript request from Python to pass me some more data, once the site was loaded already. I want to do that without moving away from the site. I know that JavaScript can do all sorts of things, but Python can be a real muscle for me with better debugging features.
Now, I'm not keen on Ajax. I know nothing about it and I have that fear factor that it would be quite complex.
TIA
Dennis
For example you can make with jQuery like this,
in controller you return rendered template:
def some_html():
return render('my_template.tpl')
and in the client side you can use jQuery
<script type="text/javascript">
('#result_from_server').load('/some_html');
</script>
,where result_from_server its can be id of wrapper div like
<div id="result_from_server"></div>
and /some_html, url for call your some_html() function.
Very good resurce for quick start with jQuery jqapi.com
I'm stumped I've looked all over the internet and found no real way to access mysql within javascript, therefore I can't do what I need to.
Basically my javascript code is loading random websites using Iframes and I would like those list of sites to be pulled from a mysql database, but I see no way in doing this. I've tried using php's include function in javascript with no luck.
Any ideas?
More information:
Im using a script I have on my server called sitelist.js for the source of the script. All that script contains is the following:
targetURLs = [
'http://www.aol.com',
'http://www.yahoo.com',
'http://www.bing.com'
];
But i would like it to load websites from the mysql database instead of having those websites.
Try something along these lines, with $sites being your array of urls from the database...
<script type="text/javascript">
var sites = JSON.parse("<?php echo addslashes(json_encode($sites)); ?>");
// now do stuff with sites.
</script>
This can't be done. Javascript (at the moment) doesn't have raw sockets. As a result, you can't call off to non-http network services like mysql. You could however wrap it using some horrible ajax API (SQL from javascript, no matter how it's accomplished, is a horrible idea).