0

My Webpage

When the user clicks the login button, it should run a python file, which is a program that checks user login info. I'm very new to coding and html so I'm not quite sure if it's possible or not. CODE

from flask import Blueprint
auth = Blueprint("auth", __name__)

@auth.route('/login')
def login():
    return "<p>Login</p>"

#Here is where I'd like to run the python script

@auth.route('/logout')
def logout():
    return "<p>Logout</p>"

2 Answers 2

1

It is possible to run a python file in html using PHP.

Write a PHP file as "index.php":

<html>
<head>
<title>RUN MY PYTHON FILES</title>
<?PHP
echo shell_exec("python test.py 'param1'");
?>
</head>

Passing the parameter to python, create a python as "test.py":

import sys
input=sys.argv[1]
print(input)

Print the parameter passed by PHP. If your parameter needs to support spaces, try doing this:

echo shell_exec("python test.py \"Param 1\"")
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, could you explain what you've done in more detail please? for example, where do I 'import sys'? In the HTML file or PHP file? And do I replace 'test.py' with the python file I want to execute?
The second part with import sys is the content of the test.py file. Just some example where you can use parameters. Yes, you change the test.py to your file name.
0

You can just create a function and let it do what you want it to do:

def your_function():
    # Put whatever it should do here

Then, when you want to run the code you wrote in your function, you can call it with. For example, if you want it to run in your login function, do this:

@auth.route('/login')
def login():
    your_function()
    return "<p>Login</p>"

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.