0

I've to run a python script from my HTML page button. When I click on the button, it should run the command "python filename.py". But I'm not getting any solutions, please help!

Thank You

1
  • You will need a server-side solution. Please look into some web frameworks such as Django and Flask. Commented Apr 24, 2018 at 5:44

3 Answers 3

2

Sorry to say but this is not possible unless your 'filename.py' is on a server e.g. flask, or else this will not work. If setup a flask server, and with a certain route it runs your code, then you can have your HTML code make a POST or GET request to this flask server, and the code should run.

Sign up to request clarification or add additional context in comments.

1 Comment

After using flask, I've successfully able to run my python script. Thanks for your suggestion @Ben10
0

Attach a listener to the button and have that listener call your endpoint:

document.querySelector('button')
  .addEventListener(() => fetch('runMyScript.php', { method: 'POST' }));

Then have that endpoint run your desired program. In PHP, for example:

<?php
// if you want the page to be able to do something with the result:
$output = `python /home/username/filename.py 2>&1`;
echo $output;
?>

Of course, a real setup better have something to validate

Comments

0

You can’t.

Python runs in the server. HTML isn’t a programming language, it’s content, and it sits in the browser. About the best you can do is use AJAX to call a function in the server (which you’d have written in Python) and, if it returns a value, return it via AJAX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.