title: Flask: How to run a function with a post method as a background task and then access it in another class
body: <p>I'm automating a pump which is turned on only when the soil humidity value(obtained from a soil humidity sensor) exceeds a certain value. This is how it is:</p>
  
  <ol>
  <li><p>A user selects a value from a dropdown (threshold value for the soil humidity)</p></li>
  <li><p>The python script continuously checks if the value from the sensor is greater than the value selected in the dropdown (this is why I need to have it running as a background task)</p></li>
  <li><p>The user can turn off the automation process by using killing the background task using an OFF button on the webpage.</p></li>
  </ol>
  
  <p>I tried the following but I got <code>working outside of request context</code> error:</p>
  
  <p><strong>web_plants.py</strong>(which calls the python soil_on.py script):</p>
  
  <pre><code>@app.route("/threshold", methods=['POST', 'GET'])
  def threshold():
      tvalue= -1 #get value from dropdown
      msg = ''
      if request.method == "POST":
             msg= "rating above 3"
             os.system("python soil_on.py&amp;")
      templateData = template(text = msg) #display text using template()
      return render_template('index.html', **templateData)
  </code></pre>
  
  <p><strong>soil_on.py</strong>(which calls the <code>auto_Irrigation()</code> function from the <code>water</code> class:</p>
  
  <pre><code>import water
  
  if __name__ == "__main__":
      water.auto_Irrigation()
  </code></pre>
  
  <p><strong>water.py</strong> (which has <code>auto_Irrigation()</code>)</p>
  
  <pre><code>app = Flask(__name__)
  
  def template(title = "HELLO!", text = ""):
  templateDate = {
      'text' : text,
      'tvalues' : getTValues(),
      'selected_tvalue' : -1
  }
  return templateDate
  
  def getTValues():
    return (10, 11, 15, 2, 1)
  
  @app.route("/", methods=['POST', 'GET'])
  def auto_Irrigation():
      tvalue= -1 #get value from dropdown
     # msg = ''
      if request.method == "POST":
          tvalue = int(request.form['tvalue'])
          if tvalue&gt; 3:
              GPIO.output(40, GPIO.HIGH)
          else:
             GPIO.output(40, GPIO.LOW)
      return render_template('index.html', **templateData)
  </code></pre>
  
  <p><strong>index.html</strong>:</p>
  
  <pre><code>&lt;h2&gt; {{text}} &lt;/h2&gt;
  &lt;form action= "{{ url_for('threshold') }}" method="POST"&gt;
  
       &lt;select name= 'tvalue'&gt;
        {% for tvalue in tvalues %}
          {% if selected_tvalue == tvalue %}
              &lt;option value="{{ tvalue }}" selected='selected'&gt;{{ tvalue }}&lt;/option&gt;
          {% else %}
               &lt;option value="{{ tvalue }}" &gt;{{ tvalue }}&lt;/option&gt;
          {% endif %}
        {% endfor %}
  
        &lt;/select&gt;
  
       &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am not sure of how to make the function as a background task. Currently this is working for me but the function is not running as a background task(so it only checks the value and turns on/off the pump):</p>
  
  <p><strong>index.html</strong>:</p>
  
  <pre><code>&lt;h2&gt; {{text}} &lt;/h2&gt;
  &lt;form action= "{{ url_for('threshold') }}" method="POST"&gt;
  
       &lt;select name= 'tvalue'&gt;
        {% for tvalue in tvalues %}
          {% if selected_tvalue == tvalue %}
              &lt;option value="{{ tvalue }}" selected='selected'&gt;{{ tvalue }}&lt;/option&gt;
          {% else %}
               &lt;option value="{{ tvalue }}" &gt;{{ tvalue }}&lt;/option&gt;
          {% endif %}
        {% endfor %}
  
        &lt;/select&gt;
  
       &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>web_plants.py:</strong>(simplified the function <code>threshold()</code> for the sake of this post to make it more understandable)</p>
  
  <pre><code>def template(title = "HELLO!", text = ""):
      templateDate = {
          'text' : text,
          'tvalues' : getTValues(),
          'selected_tvalue' : -1
      }
      return templateDate
  
  def getTValues():
      return (10, 11, 15, 2, 1) 
  
  @app.route("/threshold", methods=['POST', 'GET'])
  def threshold():
      tvalue= -1 #default value
      msg = ''
      if request.method == "POST":            
          tvalue = int(request.form['tvalue'])
          if tvalue&gt; 3:
              msg= "rating above 3"
  
      #generating template data
      templateData = template(text = msg)
      templateData['selected_tvalue'] = tvalue 
  
      return render_template('index.html', **templateData)
  </code></pre>