Flask: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= Quick tour of the python framework flask =Getting Started= <syntaxhighlight lang="py"> from app import app @app.route('/') @app.route('/index') def index():..."
 
No edit summary
Line 21: Line 21:
=Routing=
=Routing=
=Templates=
=Templates=
So flask like others such pug or egs has templates
So flask like others such pug or egs has templates. Very similar indeed
<syntaxhighlight lang="py">
from flask import render_template
from app import app
 
@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    return render_template('index.html', title='Home', user=user)
</syntaxhighlight>
And the template
<syntaxhighlight lang="py">
<html>
    <head>
        <title>{{ title }} - Microblog</title>
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
    </body>
</html>
</syntaxhighlight>

Revision as of 04:26, 23 May 2021

Introduction

Quick tour of the python framework flask

Getting Started

from app import app

@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    return '''
<html>
    <head>
        <title>Home Page - Microblog</title>
    </head>
    <body>
        <h1>Hello, ''' + user['username'] + '''!</h1>
    </body>
</html>'''

Routing

Templates

So flask like others such pug or egs has templates. Very similar indeed

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    return render_template('index.html', title='Home', user=user)

And the template

<html>
    <head>
        <title>{{ title }} - Microblog</title>
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
    </body>
</html>