Python
Flask installation , creating and deployment of website
Project
location :
1.
Install flask using pip
pip
install flask
2.
Install virtualenv
pip
install virtualenv
3.Create
a simple website ,
which
has static css and js files
a.Create
folder
b.Structure
(final)
FlaskApp
|-----------------------hello.py
|-----------------------static -------|------ css -- main.css
|------ js – – datestuff.js
|-----------------------templates –-----|-------hello.html
c.
Create hello.py
#-*-
coding: utf-8 -*- for apache webserver to under .it need in later
part
#
-*- coding: utf-8 -*-
#!/usr/bin/python
from
flask import Flask
from
flask import render_template
from
datetime import datetime
from
flask import request
app
= Flask(__name__)
@app.route('/')
@app.route('/test')
def
hello_world():
now
= datetime.now()
rule
= request.url_rule
return
render_template('hello.html', name=now ,path=str (rule)
)
it
is creating Flask app as “app” and routing / and /test .
It
is calling a “hello template” by passing name as now (current
date) and webroot as path
d.
create html template file under tempate folder:hello.html
<!doctype
html>
<head>
<link
rel="stylesheet" href="{{
url_for('static',filename='css/main.css') }}">
<script
src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script
type="text/javascript" src="{{
url_for('static',filename='js/datestuff.js') }}"></script>
</head>
<body>
<title>Hello
from Flask</title>
{%
if path == "/" %}
<h1>Home
Page</h1>
{%
endif %}
{%
if name %}
<h1>Date
from Pyton {{ name }}</h1>
<p>
with out css </p1>
{%
else %}
<h1>Hello,
World!</h1>
{%
endif %}
<p
id="demo"></p>
</body>
In
header part , it is linking css and js required.
In
body part , it is checking web path ,also displaying date which
called by main python script .
e.
create css and js files under static folder
css
:
h1
{
display:
block;
font-size:
1em;
margin-top:
0.67em;
margin-bottom:
0.67em;
margin-left:
0;
margin-right:
0;
font-weight:
bold;
}
js
$(document).ready(function(){
var
d = new Date();
document.getElementById("demo").innerHTML
= ( "From JavaScript "+ d.toDateString());
});
js
is setting date .
4.Run
the website and access it
export
FLASK_APP=hello.py
flask
run
output
:
*
Serving Flask app "hell"
*
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1
- - [03/May/2017 11:15:23] "GET / HTTP/1.1" 200 -
127.0.0.1
- - [03/May/2017 11:15:24] "GET /static/css/main.css HTTP/1.1"
304 -
127.0.0.1
- - [03/May/2017 11:15:24] "GET /static/js/datestuff.js
HTTP/1.1" 200 -
Access
from browser
4.Deploy
in appache web server
a.install
appache
yum
install httpd
b.
Install mod_wsgi
yum
install mod_wsgi
c.copy
the dev flask app folder to /var/www/ folder
-
cd Flaskapp
cd
/var/www
cp
-r /code/FlaskApp .
d.create
wgi file for apache
myapp.wgi
vi
myapp.wgi
#
-*- coding: utf-8 -*-
#!/usr/bin/python
import
sys
import
logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from
hello import app as application
Here
it is calling Flask app and imporing hello app as appliaction
e.
Edit httpd.conf
vi
/etc/httpd/conf/httpd.conf
<VirtualHost
*>
ServerName
sterinlap.company.com
WSGIDaemonProcess
yourapplication user=sterin group=sterin threads=5
WSGIScriptAlias
/ /var/www/FlaskApp/myapp.wgi
<Directory
/var/www/FlaskApp/>
WSGIProcessGroup
yourapplication
WSGIApplicationGroup
%{GLOBAL}
Order
deny,allow
Allow
from all
</Directory>
</VirtualHost>
In some linux version "WSGISocketPrefix /var/run/wsgi"
is required
is required
f.
Restart appache
-
service httpd restart
5.Access
in web
No comments:
Post a Comment