Showing posts with label study. Show all posts
Showing posts with label study. Show all posts

Thursday, August 23, 2018

[Study] Flask url_for example

Why is this needed?

A YouTube video was the first time I encountered url_for feature. I couldn't understand what it was, for what it is. And today, I began to read the official manual

http://flask.pocoo.org/docs/0.12/quickstart/#url-building

And there are a lot of example code from opensource projects
https://www.programcreek.com/python/example/51519/flask.url_for


in short, url_for() function looks up app.route('/here')
if there are more than one app.route('/here'), the function looks up right above the function

test.py

from flask import Flask, url_for, render_template

app = Flask(__name__)

@app.route("/")
@app.route("/1st")
@app.route("/2nd")
@app.route("/3rd")
def lookAtMe():
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)


index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    {{ url_for('lookAtMe') }}
  </body>
</html>

result


Tuesday, August 21, 2018

How to use render_template in Flask

Why template is needed?

Let's say we need to a number of dynamic html page. The page need to be changed based on data from outside. Without template, we need to make a static HTML page in manual way. "template" gives an easy way to replace page's content in a very simple way. it feels like a mail merge feature in a word processor.
As we want to make a dynamic page, we need to use this feature "template"

(I am following this Flask tutorial)

This instruction shows how to deal with template, url_for in Flask.

How to use "template"?

import class

in order to use "template", firstly we need to import a class named "render_template" in python file.


from flask import Flask, render_template

preparing a HTML file in "templates" folder

I prepared "hello.html" inside of templates folder.

templates/hello.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>This is hello world</h1>
  </body>
</html>


code it (test.py)

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
@app.route("/hello")
def hello():
    return render_template("hello.html")

if __name__ == "__main__":
    app.run(debug=True)


result 


Sending Data 


from flask import Flask, render_template
app = Flask(__name__)

varForPython = ["welcome", "to", "my", "channel!"]

@app.route("/")
@app.route("/hello")
def hello():
    return render_template("hello.html", varForHtml = varForPython)

if __name__ == "__main__":
    app.run(debug=True)

Receiving Data and Using it in the HTML file



<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    
    {% for line in varForHtml%}
    <h1> {{ line.name }} </h1>
    <p> {{ line.title }}</p>
    <p>{{ line.date }}</p>
    {% endfor %}

  </body>
</html>

URDF Link origin test