Jump to content

Executing python code on HTML button press

izaak_is_epic

I have a python script that I want to run upon a button press in an HTML document. There is no output from the code that is relevant to the HTML page. All I need is to run the code without refreshing the page.

The button part of the HTML looks like this:

<ul>

    <li><a onclick="runcode()">Button</a></li>

</ul>

From the hours of enraged googling I have done, I guess that I need to use a javascript/jQuery function with AJAX and some PHP to run the python.

I have tried some PHP alone to no avail.

 

For some extra context, I am modifying the min.php page from the RPi Cam Web Interface module. The idea is to have a web page that can be used to control a robot with the Pi camera's view embedded into the page. The python code outputs via the GPIO to a PICAXE chip that then activates some servo motors that will eventually control the robot. So far, getting everything working to a somewhat satisfactory standard, has been easy, with the exception of this. 

 

Thanks for reading.

Link to comment
Share on other sites

Link to post
Share on other sites

shouldn't you just create the website in Django and run python natively?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

shouldn't you just create the website in Django and run python natively?

All I'm doing is adding buttons to an already existing page that comes with the package. I need to use the pre-made js that comes packaged with the RPi Cam Web interface library for the camera stream.

 

To re-write the whole bloody thing to solve what I thought to be such a trivial problem as running a python script, server-side, at the press of a button seems ridiculous. Others have had success with PHP exec() functions but all of my attempts have failed. Given that I'm working in a PHP file, I thought I could embed some PHP into an onclick trigger, but clearly not. I don't actually need to update any content on the page so I thought that AJAX would be out of the picture but others have suggested it. 

 

The page itself looks crap. It only serves as a prototype that I can use to gain marks from.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, vorticalbox said:

shouldn't you just create the website in Django and run python natively?

Brython is also an option, you litterally only need to place a script tag referencing the brython module into your webpage, and then you can just use your typical script tag but with "text/python" instead of "text/javascript"

 

https://brython.info/

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you need to run server side script then as you said, you would need php script with exec, and ajax to create request from javascript without reloading a page, (if you need no return information then creating any get request would do but you wouldn't know if request was successful or not.

 

Other way around would be create websocket server using python, and connection to it from website, then sending command to it, and server would do whatever you want.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×