I've the myapp.py like this:
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()
I have a simple question. How to call the index() function and execute code only in the if statement (line from 8 to 13) in the Python?
I tried in this way:
>>> import myapp
>>> myapp.index()
but I get the message:
RuntimeError: working outside of request context
http://localhost:5000/works fine in a browser though? Just checking what you are really stuck on.__main__context. Ideally I would be able to reuse all the view rendering from an offline context, that is not from the flask app session but another python module which automated rendering the templates from some scheduler by writing to disk a bunch of static pages whose content was generated by the flask functions.