Using python functions in html - javascript

First of all, I am not a native English speaker, so please forgive any spelling mistakes I may have.
I am trying to integrate HTML and Python to create a GUI that can "speak" to a bus (KNX bus if you are interested).
I have a raspberry pi with an HTML page and some Python scripts, that are the ones that actually talk to the bus. I have managed to run python scripts from HTML with a bit of PHP (I am not particularly proficient in PHP), doing something like this:
if (isset($_POST ['button'])){
exec("sudo pathofpythonscript/pythonscript.py")
}
And it works just fine, when the button it's pressed, the script it's executed. But now, I want to have a script running (since I want python to be reading from the UART from the bus and display that information) and when something in the scripts happens (a condition it's met, for example), I want to be able to talk to the HTML to change a CSS property or anything else, something like this pseudo-code here
//This is just pseudo-code to try to illustrate my question, IT'S NOT working code
//In the python file, a simple toggle function
if bus_event
var != var
//in Html, with javascript
if (var_from_python_script == true){
document.getElementById('button').style.background=red;
}
If someone could tell me just how to make a simple example, with something like a python script that toggles any boolean parameter that enters and returns it, and how to "grab" that return from javascript/HTML to use it,
I think I can work everything else.
Thank you in advance!

You are confusing the HTML/CSS/Javascript code that is running in your browser and the (PHP/Python) code that is running on the server. Even if in your case, they might be the same, it doesn't matter. The python script is not running in your browser and therefore cannot change the state of your html/css or even javascript.
When you are executing your code with exec, the PHP server is running an external command and expecting a response to the immediate request. After that, it's over.
I would recommend to ditch PHP and use a Python web application framework. Flask, Django or whatever else. This will allow you to interact directly with the python code. Your web browser could then request data to your server and know what is happening. Remember, the web is normally only from the browser to the server.
Now, if you need real-time interactions, you should have a look at websockets. These will allow bidirectional communication between your browser and the running python app. There are a number of caveats with this technology but it might be better suited to your needs. Note that this will force you to write Javascript code in your browser to manage this communication but it shouldn't be too hard.

Related

How to change computer setting remotely

I would like to setup a web server on my HTPC that I can use to handle various tasks such as changing the default audio output or adjusting the display to output to only one screen without having to actually touch the machine.
While I understand the actual programming may be difficult itself, my main focus is learning how one can go about handling various events. I have initially set up a IIS server and created a site to handle all the events on my HTPC. The site is only accessible through the intranet and knowledge of the specific port. Ideally, the solution would be that you access the HTPC local ip address with the specified port and are presented with options(buttons) to either set default audio device, display etc. (This will probably involve some html programming but it is feasible to leverage a java script or is asp.net the language I should write in)
Any help would be appreciative as I know its possible to set up a server to handle events/requests - I just don't know where to begin or what to start reading up on.
if it can be done via the command line, generally it can be done via asp (on the server).
you need to give the application pool that the website will be running under permissions on your computer to be able to carry out the tasks (if you're lazy and don't care about security make it run as you)
you then simple need to write a process that starts up either a cmd process or powershell instance in the button click event handler.
For powershell, i've followed Jeff Murrs example in the past and had success.
For command line processes it is similar but you use System.Diagnostics.Process.
This should be enough for you to research and hopefully get something

Using Python to communicate with JavaScript?

Is there a way to send data packets from an active Python script to a webpage currently running JavaScript?
The specific usage I'm looking for is to give the ability for the webpage, using JavaScript, to tell the Python script information about the current state of the webpage, then for the Python script to interpret that data and then send data back to the webpage, which the JavaScript then uses to decide which function to execute.
This is for a video game bot (legally), so it would need to happen in real time. I'm fairly proficient in Python and web requests, but I'm just getting into JavaScript, so hopefully a solution for this wouldn't be too complex in terms of Javascript.
EDIT: One way I was thinking to accomplish this would be to have Javascript write to a file that the Python script could also read and write to, but a quick google search says that JavaScript is very limited in terms of file I/O. Would there be a way to accomplish this?
For security reasons, javascript in a browser is usually restricted to only communicate with the site it was loaded from.
Given that, that's an AJAX call, a very standard thing to do.
You can make HTTP requests using the XMLHttpRequest API, which Jquery abstracts with $.ajax and $.get. You can also use the lower level Websockets network API:
https://developer.mozilla.org/en-US/docs/WebSockets
Note that the XMLHttpRequest API will only allow requests to the same server, OR requests that return an appropriate Access-Control-Allow-Origin header.
It sounds like the Javascript is only going to send information, not receive any. In that case, you're in luck. I'm guessing you are also running the Javascript and the Python on the same machine.
Run a Python webserver on the machine the browser is running on. Here's a simple example:
http://webpy.org/install
Once visiting http://127.0.0.1:8080/ in your browser gives the message Hello World!, you can start adding more addresses to your website, for example http://127.0.0.1:8080/report_data, http://127.0.0.1:8080/report_event etc.
Your Javascript can then make AJAX requests using jQuery.ajax or XMLHTTPRequest, to the address http://127.0.0.1:8080/report_data, and pass the information as GET parameters.

Run program on tomcat server with Javascript, is it possible?

so, here is the thing, I need to run .jar I've programmed myself on a server.
On the otherside, that server is also a webserver, so it displays webpages and so. Those webpages are written in simple HTML and JavaScript.
So, here is the thing:
is there a way to run my program when users perform click on links in the webpage?
I created a function in Javascript, but I cannot make it work, as long as I don't know how to run shell commands in Javascript.
The execution of the program is totally transparent to the user, and what it basically does is to search content in some documents in order to update the html that is shown to the user.
Any ideas? Hope I made myself clear.
Thanks in advance!
You really can't run shell commands in JavaScript.
If I understand your question correctly, you're looking to do some sort of remote process execution.
Is your JAR file on the other server - is it running as part of a web server, I.E. as a servlet in an application container like Tomcat, such that your code can be executed by calling HTTP methods? (If not, you may want to start out with doing this.) If so, then you'll need to have your JavaScript make the HTTP call to the server to execute its code. A common way of doing this today is through the use of AJAX - and you can use something like jQuery to help with this. The response from this AJAX request (could be XML, JSON, or pre-formatted HTML) could contain the details to "update the HTML that is shown to the user".
The simplest way to do this is put the html, javascript, and a jsp page in the same war file, and then run it on tomcat. Not the best way, but definitely the quickest.
EDIT:
So to clarify, the JSP page will get invoked by the click (ajax call, use jquery), and renders some json/xml. The success handler for the ajax call takes the jsp response and updates your html page. So the jsp runs on the server, but the html/javascript runs in the browser.
If you want to make if fancier, you can use spring web-mvc and write a controller instead of a jsp page. It has the same overall effect either way.

Execute Ruby on Rails code on button click

I'm wondering how I can embed Rails code inside an html.erb so that I can run a Ruby function on the press of a button. I've googled around, and if this is not possible, how could I run a JavaScript function that updates variables inside a Ruby on Rails database?
I think we need to talk a bit about how the web works. No worries; this is confusing for a lot of people when they first start building web apps.
There are two types of code here. The first type is the server-side code, which runs on your server. In this case, it's Ruby code, and the user can't see it. This server-side code generates the second type of code, client-side code. In this case, it's Javascript code, since that's what browsers understand. The user can see it and even modify it, and it runs on the user's computer.
Since the database lives on your server, and is not publicly accessible (after all, wouldn't that be pretty bad?), you will have to use server-side code to interact with the database. Code that runs on the user's browser cannot directly interact with the database, so you'll have to use a kind of "glue" technology here.
One way of doing that is an AJAX request. This is when your Javascript code tells the browser to send a message to your server and wait for a response. This could happen on, say, a button click. Then, the server can handle the request as usual, interact with the database, whatever, and send the result back to the browser. To the user, however, all they see is that clicking the button takes the action they expect, and that's all that matters.
There are a few ways to go about implementing AJAX in a Rails application. Rails has a few built-in methods to look into, like button_to_remote in Rails 2 or button_to with the :remote option in Rails 3, that can help simplify the process. Or, you can write the AJAX code yourself, and even Googling "AJAX tutorial" would be a good starting point. I'd start with the Rails helpers, though, since they usually make things pretty simple for beginners.
Good luck!
You could use an JS AJAX request to request a backend ERB file, which then accesses the database.
Using AJAX with JS
Using AJAX in jQuery

Write a serverside c++/openGL App, that is accessible via JavaScript

I am currently having an idea where I want to save an image from a c++/openGL application on demand from a browser. So basically I would like to run the application itself on the server and have a simple communication layer like this:
JS -> tell application to do calculations (and maybe pass a string or some simple data)
application -> tell JS when finished and maybe send a link, text or something as simple as that.
I don't really have alot of experience with webservers and as such don't know if that is possible at all (it's just my naive thinking). And note: I am not talking about a webGL application, I just want to have simple communication between a c++ serverside application, and the user.
Any ideas how to do that?
Thanks alot!
Basically no matter what your language/framework you choose for your web server, you just need a interface that is callable from your browser JS, and you can do whatever you want in the server once it recieves the call.
Most likely any web service interface exposed from the server.
Just need to safeguard your server not to get DoS since it sounds like it's a huge process.
As far as I know, JavaScript (at least when embedded in HTML) is executed on your local machine and not on the server so that there is IMHO no way to directly start your server-application using JS.
PHP for example is executed on the server-side and so you could use e.g. the php system function to call your C++/OpenGL application on the server - initiated on demand through a web-browser.
When the call is finished you could then directly present the image.
Well you could always use the cgi interface to invoke your application
and have it save that image somewhere accessible to the webserver.
Then have your js load that via ajax.
Or make a cgi App that talks to the app and then serves a small
page with the pic in it.
[EDIT]
Answering the comments:
CGI is not complex to learn, it is mostly a simple convention
you can follow. I think it would give you the maximum of
flexibility. I don't know which php mods allow you to leave the cozy protection of the server-application and interact with other stuff on your server.

Categories

Resources