title: How to call some function from the Flask app in Python?
body: <p>I've the <code>myapp.py</code> like this:</p>
  
  <pre><code>from flask import Flask
  from flask import request
  from flask import render_template
  app = Flask(__name__)
  
  @app.route('/', methods=['GET', 'POST'])
  def index():
      if request.method == 'POST':
           # do something
           # for example:
           message = 'I am from the POST method'
           f = open('somefile.out', 'w')
           print(message, f)
  
      return render_template('test.html', out='Hello World!')
  
  if __name__ == '__main__':
      app.run()
  </code></pre>
  
  <p>I have a simple question. <strong>How to call the <code>index()</code> function and execute code only in the <code>if</code> statement (line from 8 to 13) in the Python?</strong></p>
  
  <p>I tried in this way:</p>
  
  <pre><code>&gt;&gt;&gt; import myapp
  &gt;&gt;&gt; myapp.index()
  </code></pre>
  
  <p>but I get the message:</p>
  
  <pre><code>RuntimeError: working outside of request context
  </code></pre>