title: Connecting python script with html button and flask
body: <p>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.</p>
  <p>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?</p>
  <p>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.</p>
  <p>Edit: not looking for a flask tutorial. I've tried 3things:</p>
  <ol>
  <li>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</li>
  </ol>
  <pre class="lang-html prettyprint-override"><code>&lt;form action=&quot;/pic&quot; method=&quot;POST&quot;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;GET THE SCRIPT&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>in my main.py flask file</p>
  <pre class="lang-py prettyprint-override"><code>@app.route('/pic', methods=['GET', 'POST'])
  def pic():
      os.system(&quot;python file.py&quot;) #file.py is the script I'm trying to start
      return &quot;done&quot;
  </code></pre>
  <ol start="2">
  <li>Doesn't do anything at all.
  in html file:</li>
  </ol>
  <pre class="lang-html prettyprint-override"><code>    &lt;input type=&quot;button&quot; id='script' name=&quot;scriptbutton&quot; value=&quot; Run Script &quot; onclick=&quot;goPython()&quot;&gt;
      
     &lt;script src=&quot;http://code.jquery.com/jquery-3.3.1.min.js&quot; integrity=&quot;sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  
      &lt;script&gt;
          function goPython(){
              $.ajax({
                url: &quot;/scripts/file.py&quot;,
               context: document.body
              }).done(function() {
               alert('finished python script');;
              });
          }
      &lt;/script&gt;
  </code></pre>
  <ol start="3">
  <li>I get a GET &quot;/scripts/file.py HTTP/1.1&quot; 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.</li>
  </ol>
  <p>in html</p>
  <pre class="lang-html prettyprint-override"><code>
  &lt;form  action=&quot;/run&quot; method = &quot;POST&quot;&gt;
       &lt;input type=&quot;button&quot; id='script' name=&quot;submit&quot; value=&quot;Run Scripttttttt&quot;&gt;
      &lt;/form&gt;
  </code></pre>
  <p>in flask main.py</p>
  <pre class="lang-py prettyprint-override"><code>@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() + '&lt;br/&gt;\n'
  
      return Flask.Response(inner(), mimetype='text/html') 
  </code></pre>