Python: Flask Base

From delarco wiki
Jump to navigation Jump to search

app.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello():
    return render_template("home.html", message="Hello Flask!")

@app.route('/about')
def about():
    return "about.... my page"

@app.route('/produtos')
def products():
    return render_template("products.html",numbers=[1, 2, 3, 4, 5], nome='leandro')

'''
@app.route('/<name>')
def hello_name(name):
    return "Hello {}!".format(name)

@app.route("/about")
def about():
    return """
    <h1 style='color: red;'>I'm a red H1 heading!</h1>
    <p>This is a lovely little paragraph</p>
    <code>Flask is <em>awesome</em></code>
    """
'''
if __name__ == '__main__':
    app.run(port=12345)


templates/home.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Index</title>

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</head>

<body>
  <h1 style="color: blue">Index</h1>
  <p>This is an HTML file served up by Flask</p>

  <p>Message: {{ message }}</p>

  <p>Counter:</p>
  <ul>
      {% for n in numbers %}
      <li>{{ n }}</li>
      {% endfor %}
  </ul>

  <hr />

  <a href="/about">about</a>

</body>

</html>

templates/index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Index</title>

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</head>

<body>
  <h1 style="color: blue">Index</h1>
  <p>This is an HTML file served up by Flask</p>

  <p>Message: {{ message }}</p>

  <p>Counter:</p>
  <ul>
      {% for n in numbers %}
      <li>{{ n }}</li>
      {% endfor %}
  </ul>

  <hr />

  <a href="/about">about</a>

</body>

</html>