After all, when I got to know python, I thought I had to output it somehow, and when I was looking for a python web framework, I came up with Flask and django.
It seems that there are many other things.
Reference: The hottest Python in 2016! Thorough comparison of three web frameworks http://www.sejuku.net/blog/3713#Python
If you make a mistake at the time of building the environment, you will lose your motivation at once, so I would like to try "Flask", which seems to be relatively easy to introduce.
CentOS 7
** Install virtualenv **
Common to python2.7 series and 3.5 series
easy_install virtualenv
You can also use pip
# pip3 install virtualenv
Next, use Flask to create a directory to display the page.
# mkdir test
# cd test
Next, build an environment called env with virtualenv
# virtualenv env
Then activate this environment.
# . env/bin/activate
# easy_install Flask
By the way, it can also be installed with pip.
# pip install flask
Create hello.py and make the directory structure as shown below.
test ┣ env ┗ hello.py
#hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run()
Do this.
# python hello.py
* Running on http://localhost:5000/ (Press CTRL+C to quit)
Check directly from your local web browser. Enter ttp: // localhost: 5000 / or ttp: //127.0.0.1:5000/ in the address bar to open it.
If the above is successful, I would like to continue loading the "HTML" file.
test ┣ env ┣ templates ┃ ┗ world.html ┗ hello.py
Build a directory like the one above.
Prepare the HTML to be loaded this time.
#world.html
<!doctype html>
<title>templates Hello World</title>
<h1>TEMPLATES Hello World</h1>
To load world.html, have hello.py import render_template to load the templates directory.
#hello.py
from flask import Flask,render_template #<-- render_Load template
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('hello.html')
if __name__ == '__main__':
app.run()
By tweaking the @ app.route ('/') part of hello.py earlier, you can make it behave as if there is a lower directory. I will actually try it.
#hello.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/world/') #<--Load as a virtual world directory
def hello():
return render_template('hello.html')
if __name__ == '__main__':
# app.debug = True
app.run(host='0.0.0.0')
In the above, @ app.route ('/') is changed to @ app.route ('/ world /').
Let's change the HTML so that you can see that it has also changed.
#world.html
<!doctype html>
<title>templates Hello World</title>
<h1>WORLD Hello World</h1>
I will try it.
# python hello.py
* Running on http://localhost:5000/ (Press CTRL+C to quit)
At ttp: //127.0.0.1:5000/, it will be Not Found and will not be displayed.
It will be displayed at ttp: //127.0.0.1:5000/world/.
Since it is on the Web, it is meaningless if it can only be seen on the server local PC. Tweak hello.py a little. Modify the last code, app.run ().
app.run()
app.run(host='0.0.0.0')
Now you can access it from other PCs. Of course, it's a matter of course, but please keep port 5000 on the firewall open.
When I created "hello.py", I thought it would be fine, so I named it "flask.py".
I'm using this name elsewhere, so I get an error saying that flask can't be imported.
# python flask.py
Traceback (most recent call last):
File "flask.py", line 1, in <module>
from flask import Flask
File "/root/test/flask.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name 'Flask'
It was a coincidence, but I learned one thing.
Recommended Posts