Hello, Flask! Can be done in seconds, but people who don't know the contents

Introduction

When you try to start flask, create a local host first and display "hello, flask!" There. However, many people may not understand the contents of the code at that time. So, I wrote down the meaning of each one.

For the time being, to Hello, Falsk

environment

Windows PC Visual Studio Code ver.1.45.1 Python 3.8

installing flask

  1. Open cmd and enter pip3 install flask to install without permission.
  2. Open vscode. (See other people's article about building the environment in vscode)
  3. Create the following python code ʻapp.py`.

python : app.py

app.py


# coding: utf-8
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, Flask!"

if __name__ == "__main__":
    app.run()
  1. Access the local host (http: // ~) that appears after that with a browser.
  2. There
helloflask1.png

A screen like this appears. (I wonder if it was a hindrance to the margins ...)

About app.py

app.py


# coding: utf-8
from flask import Flask

app = Flask(__name__)

@app.route("/test")
def hello():
    return "Hello, Flask!"

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

What are you doing with this code

from flask import Flask

For, load a module called flask. If you talk about the contents of flask, the story seems to be out of sync, so I will omit it here. At this time,

Flask(__name__)

In this code, name is a variable that is defined arbitrarily. Contains the module name of the file. When the file is run directly as a script, name becomes main.

import Flask

In that case, the following code

flask.Flask(__name__) 

It is easier to write from the beginning because you will have to specify.

'''python @app.route("/test")

`@app.route()`Is for associating a URL with a function. This is something that gets involved when you associate it with html etc., so you can't say anything by itself.
The rest of the code content is omitted because it is the basic part of python.

#at the end
I've just started myself, so please let me know if you make any mistakes.


Recommended Posts

Hello, Flask! Can be done in seconds, but people who don't know the contents
I was in charge of maintaining the Fabric script, but I don't know.> <To those who
Python beginners tried Hello World in 30 seconds using the micro-framework Flask