How to execute Python scripts from HTML button - php

My html page has a button and when I click that button I need to execute a python file. I don't need to show the python file content in browser, I only need to execute the python file by clicking the html button. When executing the python file, it will generate a json file which I need.
This is my code and it's opening the python file but not showing any output.
<html>
<body>
<h1> DEVELOPMENT </h1>
<form action="phpfile.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
</body>
</html>
<?php
$result_last_line = system("python C:/xampp/htdocs/NEW/pythonfile/identifyrooms.py");
header('Location: view.html');
exit;
?>
When I run my python file using editor the output console is as below.
Can anyone find the error?.
Thanks

Related

I want to run python file(program which generates 10 blank text files) when clicked on button in Php

I have a python file which generates 10 blank files. I have another php file with html button. When the button is pressed the python script should run.
When I run python file on terminal, it works fine and generates blank files.
But when i click button on php, it does nothing.
Can anyone help me out with this problem?
The php and python files are attached below.
upload.py:
for i in range(0,10):
f=open(str(i)+"upload.txt", "a")
f.write("This is line")
f.close()
upload.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python upload.py");
echo"success";
}
?>

How to execute python script from php?

I'm new to php and html (internet languages in general) and I would like to execute a python script that takes a picture from my php page.
I did some research, edited some code (according to my understanding) and came up with this, but it doens't do anything.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="call">Click</button>
</form>
</body>
</html>
<?php
if(isset($_POST['sub']))
{
exec('sudo python take_picture.py');
echo "Picture captured";
}
?>
I managed to execute the python script from a html page invoking a simple php script separately, but when it takes the picture it sends me to a new page (blank page with the text displayed) which I don't want. Here are the codes:
HTML button:
<form action="take_picture.php" method="post">
<button>Click</button>
</form>
PHP script:
<?php
exec('sudo python take_picture.py');
?>
What I need is to simply press the "click" button and take the picture without sending me to anywhere else.
Could you please guys aid me to achieve what I need and explain with apples what I was doing wrong or what I missed.
Thank you in advance!
You can make an :
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
More information on this Stack Overflow Question.
Running a Python script from PHP

How can my PHP code run when i submit a HTML form

I am trying to make a website using python and in flask I have a webpage that has a submit button, when the button is pressed I need it to run a python file I have called Answers.py
from flask import Flask, request, render_template, send_from_directory
app = Flask(__name__)
#app.route('/help', methods=["GET","POST"])
def help():
return render_template("help.html")
the HTML code on the page is this and i am trying to get when the button is pressed it runs a php script that outputs the value from a separate python file, the output text.
<html>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="Hello world">Submit
call</button>
</form>
</body>
</html>
I have tried this php code in the same file as the HTML but nothing is output when I press the button, i'm new to php so i'm not sure what the problem is.
<?php
if(isset($_POST['sub']))
{
$result = exec("Python Path Answers.py /tmp");
echo $result;
}
?>
The php script never gets referenced in the html page!
<form action="" method="post"><!-- action there is empty! -->
You should reference the php script in the action attribute of the <form> tag, like so:
<form action="myscript.php" method="post">

HTML form button to run PHP to execute Python script

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.

Run python script on a php android server

I have a webserver in Android using KSWEB application. It can support php, mysql, etc. I also have installed SL4A and python interpreter on my Android. I want to execute a python script (that runs on server side, the Android) on the click of a button.
All I have for now, is this:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<button name=test>TEST</button>
</body>
<?php
if(isset($_POST['test']))
{
exec('python test_andrei.py');
echo ('Worked!');
}
?>
</html>
Of course nothing happens when I click the button (both on server side (no script executed) and on client side (no echo output))... I am a complete noob in php and python...
Thank you for your help!
First of all wrap the name value in quotes for the button:
<button name="test">TEST</button>
Secondly you need to wrap the button (and any other inputs) in a form for that isset($_POST['test']) to be submitted as expected:
<form method="post" action="<?php echo html_entities($_SERVER['PHP_SELF']); ?>">
<button type="submit" name="test">TEST</button>
</form>
and also as you can see i added the action to the form so it submits to the same page

Categories