from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
/application.py
/templates
/hello.html
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
import unittest
from application import app
class HelloWorldTest(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_hello(self):
response = self.client.get('/hello')
self.assertEqual(response.data, 'Hello World!')
"Certain objects in Flask are global objects, but not of the usual kind. These objects are actually proxies to objects that are local to a specific context."
from flask import request
@app.route('/hello', method='POST')
def hello():
assert request.path == '/hello'
assert request.method == 'POST'
Seriously?!