Launch a web server with Python and Flask

――I've run a simple Python script or a simple program on Google App Engine, but I've never run a web program on its own, so I'll try it.


――Since it is a copy and paste from various places, there may be extra work. --The environment is Mac (El Capitan). --The brew and pip commands were included before.

Install pyenv

--It is a version switching environment for python. Python itself will be installed in the user directory.

% brew link autoconf pkg-config
% brew install pyenv
% brew install pyenv-virtualenv

--If writing under / usr / local / is prohibited, change the ownership etc.

Install virtualenv

--It seems to be a (?) Command that virtualizes the entire environment. --The virtualenv command seems to be only for python2 series, and it seems that 3 series is a different command, but for the time being, with virtualenv

%  pip install virtualenv

python settings

Environment variable

--If you leave it as it is, it will use the system python, so change the environment variable to use pyenv's python. --Since I am zsh, I write it in .zshrc, but if it is standard, write it in .bash_profile.

% echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
% echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
% echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Installation of each version

--For the time being, try installing the versions 2.7.11 and 3.5.1.

% pyenv install 2.7.11
% pyenv install 3.5.1

--Confirmation of installation.

% pyenv versions                                                                           [~]
* system (set by /Users/user/.python-version)

--According to the system setting, there are three explicit numbers, 2.7.11 and 3.5.1.

Program settings

--First, create a program directory to run the system and move to it.

% cd /[path]/proj

--Here, create a virtual environment with the virtualenv command.

% virtualenv testapp

-/ [path] / proj / testapp <-This will be the program folder with a nice environment.

--Switch to the virtual environment in testapp. The shell also has (testapp), so you can see that it has switched.

% source testapp/bin/activate
(testapp) [user]%    

Library installation

--Enter Flask.

(testapp) [user]% pip install Flask

Collecting Flask
  Using cached Flask-0.11.1-py2.py3-none-any.whl
Collecting click>=2.0 (from Flask)
Collecting Werkzeug>=0.7 (from Flask)
  Using cached Werkzeug-0.11.10-py2.py3-none-any.whl
Collecting Jinja2>=2.4 (from Flask)
  Using cached Jinja2-2.8-py2.py3-none-any.whl
Collecting itsdangerous>=0.21 (from Flask)
Collecting MarkupSafe (from Jinja2>=2.4->Flask)
Installing collected packages: click, Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask
Successfully installed Flask-0.11.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.10 click-6.6 itsdangerous-0.24

(testapp) [user]% pip list
click (6.6)
Flask (0.11.1)
itsdangerous (0.24)
Jinja2 (2.8)
MarkupSafe (0.23)
pip (8.1.2)
setuptools (23.0.0)
Werkzeug (0.11.10)
wheel (0.29.0)

A group of files was created like this.

(testapp) [user]% tree -L 2
└── testapp
    ├── bin
    ├── include
    ├── lib
    └── pip-selfcheck.json

4 directories, 1 file

I will put such a program.

# coding: utf-8

from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello World!'

def hello_world_ja():
    return 'Hello World!'

if __name__ == '__main__':

――I don't understand the rules yet, so I put it in app / like this.

(testapp) [user]% tree -L 3                                              [~/Desktop/python-dev/proj]
└── testapp
    ├── app
    │   └──
    ├── bin
    ├── include
    ├── lib
    └── pip-selfcheck.json

I will do it!

(testapp) [user]% python testapp/app/
 * Running on (Press CTRL+C to quit)

――It seems that it worked on 5000 ports.

Operation check

--You can use a browser, but open another terminal and check.

% curl http://localhost:5000                                      
Hello World!
% curl http://localhost:5000/ja
Hello World!

――It worked ~

Recommended Posts

Launch a web server with Python and Flask
Start a simple Python web server with Docker
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Launch a Python web application with Nginx + Gunicorn with Docker
A server that echoes data POSTed with flask / python
Creating a Flask server with Docker
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Until you install Python with pythonbrew and run Flask on a WSGI server
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Library for specifying a name server and dig with python
Easy deep learning web app with NNC and Python + Flask
POST variously with Python and receive with Flask
A memo with Python2.7 and Python3 on CentOS
Daemonize a Python web app with Supervisor
[Python] A quick web application with Bottle!
Create a simple web app with flask
Launch a Flask app in Python Anywhere
Easy web app with Python + Flask + Heroku
Run a Python web application with Docker
Let's make a web framework with Python! (1)
Create a web service with Docker + Flask
Practice web scraping with Python and Selenium
Easy web scraping with Python and Ruby
Let's make a web framework with Python! (2)
Build a detonation velocity website with Cloud Run and Python (Flask)
I made a Nyanko tweet form with Python, Flask and Heroku
Building a python environment with virtualenv and direnv
Python Web Content made with Lolipop cheap server
Vienna with Python + Flask web app on Jenkins
Create a web map using Python and GDAL
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Start a web server using Bottle and Flask (I also tried using Apache)
Local server with python
Programming with Python Flask
I want to make a web application using React and Python flask
I made a Mattermost bot with Python (+ Flask)
Extract data from a web page with Python
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
WEB scraping with python and try to make a word cloud from reviews
Create an animated GIF local server with Python + Flask
Build a python virtual environment with virtualenv and virtualenvwrapper
Easy machine learning with scikit-learn and flask ✕ Web app
Let's make a simple game with Python 3 and iPhone
Build a python virtual environment with virtualenv and virtualenvwrapper
[Python] I tried running a local server using flask
Create a fake Minecraft server in Python with Quarry
[Vagrant] Set up a simple API server with python
HTTP server and HTTP client using Socket (+ web browser) --Python3
Programming with Python and Tkinter
Encryption and decryption with Python
Web scraping with python + JupyterLab
Python and hardware-Using RS232C with Python-
Install Python and Flask (Windows 10)
Web application development with Flask
Web API with Python + Falcon
Make a fortune with Python
Easy HTTP server with Python