Flask-Python realization web page

Example child 1: The most simple web movement

The most simple reality hello world

hello.py


# start source block
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
	return 'Hello World!'

def main():
	#Web service device for development
	app.run(host='127.0.0.1', port=5678, debug=False)


if __name__ == '__main__':
	#Main function function for adjustment
	main()
# end source block

上面代码保存为hello.py Command line $ python hello.py Residential browser site: http: // localhost: 5678 Normal surface display Hello World!

Example child 2: Blueprint behavior views py text sum html model board

Catalog structure

hello/ --app.py --views/ ----__ init__.py # Action Included ----view_hello.py --templates/ ----view_hello.html

Main action app.py text

app.py


# start source block
from flask import (
    Flask,
    Blueprint,
)

#Introductory objective view
from views import view_hello

app = Flask(__name__)

#Note: Roots of the city, owned by the object, and owned by the object._url/hello/Appearance
app.register_blueprint(view_hello.app, url_prefix='/hello')


def main():
    #Web service device for development
    app.run(host='127.0.0.1', port=5678, debug=False)


if __name__ == '__main__':
    #Main function function for adjustment
    main()
# end source block

html数据先是用服务器端处理view_hello.py

view_hello.py


# start source block
from flask import (
    Flask,
    Blueprint,
    render_template,
)

app = Blueprint(
    'views.hello',
    __name__,
    template_folder='templates',
)


@app.route('/sayhello')
def say_hello():
    page_title = 'hello world'
    page_html = 'view_hello.html'

    res = {
        'page_title': page_title,
        # 'data_lst': data_lst,Other data display number
    }

    return render_template(
        page_html,
        res=res,
    )
# end source block

__init__.py
# start source block
# package
#Comment
# end source block

View_hello.html for front edge display

view_hello.html


<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>{{res.page_title}}</title>

  </head>
  <body >
The world of luck!
  </body>
</html>

Command line $ python app.py Sightseeing browser site: http: // localhost: 5678 / hello / sayhello Normal surface display Theme: hello world! 页 Surface content: 你子 world!

Recommended Posts

Flask-Python realization web page
Web page summary (preprocessing)
Web scraping with BeautifulSoup4 (layered page)
[Personal note] Web page scraping with python3
Monitor web page updates with LINE BOT
Web scraping with BeautifulSoup4 (serial number page)