0

I am trying to create a webapp and am fairly new to it. I have a python script(file.py) that transforms data selected by a user. It handles all the inputs and outputs.

I am using flask(main.py) for the server part of it and html. I want to place a button in my html code so it will start the execution of the file.py. Can anyone assist me with an example setup for the connections between the 3?

I've looked at other examples but I'm unable to recreate it as they're doing different things. Also, file.py is fairly large so I want to avoid putting it into a function.

Edit: not looking for a flask tutorial. I've tried 3things:

  1. A shell pops up for half a second but the disappears. Then I'm redirected to a page which just has the text in my return statement in my html file
<form action="/pic" method="POST">
    <input type="submit" value="GET THE SCRIPT">
</form>

in my main.py flask file

@app.route('/pic', methods=['GET', 'POST'])
def pic():
    os.system("python file.py") #file.py is the script I'm trying to start
    return "done"
  1. Doesn't do anything at all. in html file:
    <input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="goPython()">
    
   <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

    <script>
        function goPython(){
            $.ajax({
              url: "/scripts/file.py",
             context: document.body
            }).done(function() {
             alert('finished python script');;
            });
        }
    </script>
  1. I get a GET "/scripts/file.py HTTP/1.1" 404 message. I have my scripts folder in the same directory as my templates folder. Also tried placing the scripts folder inside the templates folder.

in html


<form  action="/run" method = "POST">
     <input type="button" id='script' name="submit" value="Run Scripttttttt">
    </form>

in flask main.py

@app.route('/run',methods=['GET', 'POST'])
def index():
    def inner():
         proc = subprocess.Popen(
            ['python file.py'],             
            shell=True,
            stdout=subprocess.PIPE
        )
         for line in iter(proc.stdout.readline,''):
            time.sleep(1)                          
            yield line.rstrip() + '<br/>\n'

    return Flask.Response(inner(), mimetype='text/html') 
A.G
  • 17
  • 1
  • 6
  • This is too broad to be covered in one answer on stack overflow. You should look up tutorials on building web applications. Maybe a good flask tutorial covers all you need. You also need to learn about html forms and understand at least the basics of how http works. – zvone Jul 29 '20 at 18:43
  • Start here https://www.youtube.com/watch?v=MwZwr5Tvyxo and complete all the videos. Then come back if you are still unsure. Corey will give you all the tools i believe you will need to answer your question. – Lewis Morris Jul 29 '20 at 18:50
  • Your button can be a part of a "form", have a look at: https://flask-wtf.readthedocs.io/en/stable/quickstart.html#creating-forms – Daniser Jul 29 '20 at 18:54

2 Answers2

0

You can set up a Flask endpoint that your button can send a request to. Then let the endpoint's function call your python script.

Have a look at this discussion about Flask - Calling python function on button OnClick event and this discussion about How can I make one python file run another? to get you started.

Elisabeth Strunk
  • 516
  • 1
  • 6
  • 15
0

Using an HTML Anchor tag (i.e ) is the easiest way. Here is an example:

<a href="your_flask_route">This is a link</a>

But since you've chosen button, JavaScript will come in handy. Here's an example(inline):

<button onclick="window.location.href='your_flask_route';">
  This is a link
</button>

and then in your flask main.py file you should have this:

@app.route('/your_flask_route')
def your_flask_route():
    '''some lines of code'''
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
  • I tried adding an os.system to execute the python script. A shell pops up momentarily then I'm redirected to a page just showing text in my return statement. – A.G Jul 31 '20 at 14:34
  • I used subprocess.call... in my function instead of os.system... and it worked using your example. Thank you very much – A.G Aug 04 '20 at 19:16