Skip to content

Commit d8f06fb

Browse files
author
Systemaker
committed
Update README for easy read
1 parent 9caff72 commit d8f06fb

File tree

1 file changed

+114
-121
lines changed

1 file changed

+114
-121
lines changed

README.md

Lines changed: 114 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ Python Flask WEB API DEMO
3636
- CUSTOM THEME layout and templates
3737
- Logger setting service
3838
- INTERNATIONALIZATION with functions like get_locale() and get_timezone() (flask-babel optional) based on :
39-
current_user locale and current_user timezone
39+
current_user locale and current_user timezone
4040
or global current_language and global timezone
4141
or browser language and locale timezone
4242
- Helpers (include decorators like SSL required, threaded function, Datetime and timezone utils, datetime format filter, random populate data, Random token generator)
43-
- Maintenance/coming-soon, flash-message page
43+
- Maintenance/coming-soon, flash-message page
4444
- Error handlers : 404 (path not found),
45-
500 (server error),
46-
403 (forbidden page or invalid csrf token form),
47-
400 (Bad request, the syntax of the request entity is not correct),
45+
500 (server error),
46+
403 (forbidden page or invalid csrf token form),
47+
400 (Bad request, the syntax of the request entity is not correct),
4848
422 (Unprocessable Entity : the request is syntactically correct but his contained instructions is
4949
semantically erroneous so it was unable to process )
5050

@@ -62,77 +62,42 @@ Python Flask WEB API DEMO
6262
- `python run.py` -> http://server_ip:5000
6363

6464
#### Use it with configuration environment variable :
65-
- `export FLASK_CONFIG=development`
66-
or on Windows systems shell script `set FLASK_CONFIG=development`
67-
- `export FLASK_APP=run.py`
65+
- `export FLASK_CONFIG=production`
66+
or on Windows systems shell script `set FLASK_CONFIG=production`
67+
- `export FLASK_APP=run.py`
6868
or on Windows systems `set FLASK_APP=run.py`
6969
- `flask run`
7070

71+
##### Customize config:
72+
- check the `config.py`file or your secret config `instance/config.py` file
7173

72-
#### PRODUCTION CONFIG with GUNICORN : Use it for production with GUNICORN Upstart script and NGINX config from utils directory :
73-
74-
- set environment to production with `export FLASK_CONFIG=development`
75-
or on Windows systems shell script `set FLASK_CONFIG=development`
76-
- active your Python virtual environment
77-
- GUNICORN : on shell command type `gunicorn --bind 0.0.0.0:5000 run:app`
78-
'run' is the name of your main application file 'run.py' which serve as the entry point for your application
79-
'app' is here the name of your defined application in app/__init__.py
80-
- If you visit your server's domain name or IP address with :5000 appended to the end in your web browser, you should see the homepage of your application
81-
- Create an Upstart script which will allow server to automatically start Gunicorn and serve our Flask application whenever the server boots :
82-
- create Gunicorn Upstart script : `sudo nano /etc/init/myproject_gunicorn.conf`
83-
or customize Upstart script file (edit example in file 'myproject_gunicorn.conf' in utils directory and replace 'myproject' paths by your project path) (replace user keyword by your chosen server user name ) and place it in /etc/init/myproject_gunicorn.conf
84-
- then type `sudo start myproject_gunicorn` (replace myproject with your application folder name)
85-
- After editing a code, you can refresh app by typing `sudo service myproject_gunicorn restart`
86-
- NOTE : with our Gunicorn config upstart script file, you can generate Python and Gunicorn error logs file and access logs file in your project folder 'myproject/logs'
87-
88-
- NGINX: then create a new server block configuration file in Nginx's sites-available directory.
89-
`sudo nano /etc/nginx/sites-available/myproject`
90-
- then customize it : see example in file 'myproject_nginx.txt' in utils directory
91-
- Then enable the Nginx server block configuration, link the file to the sites-enabled directory:
92-
`sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled`
93-
- test for syntax errors by typing: `sudo nginx -t`
94-
- restart the Nginx process to read the our new config: `sudo service nginx restart`
95-
96-
#### PRODUCTION CONFIG with TORNADO : Use it for production with TORNADO SERVER, SUPERVISOR Upstart script and NGINX config from utils directory :
97-
- todo :
98-
Supervisor Upstart config script
99-
Tornado server config script
100-
Nginx server config script
101-
Redis server-side session config script
102-
103-
104-
#### CORS CONFIG FOR FRONTEND APP :
105-
- edit origins url in "register cors" section in __init__.py file :
106-
CORS(app, resources={r"/*": {"origins": "http://localhost:3000"}})
107-
74+
##### Customize templates edit `/app/templates/base.html`:
10875

109-
### REDIS CONFIG FOR SERVER-SIDE SESSION WITH FLASK SESSION :
110-
- Install, configure and secure Redis in your server : https://redis.io/
111-
- Add in your config.py file or prod-config.py file :
112-
import redis
113-
...
114-
SESSION_TYPE = 'redis'
115-
SESSION_REDIS = redis.from_url('127.0.0.1:6379')
116-
76+
> <!DOCTYPE html>
77+
> {% set bootstrap_version = '3.3.4' %}
78+
> {% set jquery_version = '2.1.3' %}
79+
> {% set modernizer_version = '2.8.3' %}
80+
> {% set bootswatch_version = '3.3.2' %}
81+
> {% set bootswatch_theme = 'slate' %}
11782

118-
#### COMMON SQL SCRIPT ON DATABASE FROM MYSQL SERVER :
119-
- Signin in first with `mysql -u root -p`
120-
- Create a database : `CREATE DATABASE my_database;`
121-
- Show all databases : `SHOW DATABASES;`
122-
- Select my database : `USE my_database;`
123-
- Execute script on my database, for example insert data into a mysql table :
124-
`INSERT INTO table_name (field1, field2, ...) VALUES (value1, value2, ...);`
125-
- Import and execute sql script file :
126-
`source /home/myproject_path/data/schema_mysql.sql;`
127-
- View current database : `SELECT database();`
128-
- Delete my database : `DROP DATABASE my_database;`
83+
In case you don't like the "slate" theme, you can chose a nice theme from http://bootswatch.com/ and just replace the theme name
12984

85+
##### authorization control acces with Flask-login :
86+
- in template, use current_user : {% if current_user.is_authenticated %} ... {% else %} ... {% endif %}
87+
- in controllers route, use `@login_required` to check if user is already login then `current_user` to check his role
88+
from flask_login import login_required, current_user
89+
@auth_page.route('/dashboard')
90+
@login_required
91+
def dashboard():
92+
# prevent non-admin roles from accessing the page
93+
if not(current_user.is_admin):
94+
abort(403)
95+
return render_template('auth/dashboard.html')
13096

131-
#### IMPORT SQL SCHEMA DATABASE SAMPLE IN MYSQL:
132-
`mysql -u root -p DB_NAME < /home/myproject_path/data/schema_mysql.sql`
13397

134-
#### EXPORT SQL SCHEMA DATABASE FROM MYSQL:
135-
`mysqldump -u root -p --databases DB_NAME > /home/myproject_path/data/schema_mysql.sql`
98+
---------------------------------------------------------------------------------------------------------
99+
-------------- SECTION PYTHON VIRTUAL ENVIRONMENTS SETTING
100+
---------------------------------------------------------------------------------------------------------
136101

137102
#### About python virtual environment : how to manage it in local project directory:
138103

@@ -156,7 +121,7 @@ Python Flask WEB API DEMO
156121
# Activate this environment for your current shell session
157122
`workon [<name>]`
158123
or `source my_project/bin/activate`
159-
or on Windows go in the Scripts path folder `cd my_project/env/env1/Scripts` then `activate`
124+
or on Windows go in the Scripts path folder `cd my_project/env/libs1/Scripts` then `activate`
160125

161126
- WARNING ON WINDOWS SYSTEMS : Some paths within the virtualenv are slightly different on Windows: scripts and executables on Windows go in ENV\Scripts\ instead of ENV/bin/ and libraries go in ENV\Lib\ rather than ENV/lib/.
162127
on Windows systems, the equivalent activate script is by opening active shell in the Scripts folder (Based on your active shell (CMD.exe or Powershell.exe), Windows will use either activate.bat or activate.ps1)
@@ -169,75 +134,101 @@ Python Flask WEB API DEMO
169134
# Remove a virtual environment
170135
`rmvirtualenv [<name>]`
171136

137+
##### To install a new package and save it on requirement file:
138+
`python -m pip install <new_package> && pip list > requirements.txt && pip list --format=freeze > requirements-pip2.txt`
172139

173-
##### Customize config:
140+
##### To install all packages:
141+
`pip install -r requirements.txt` or `python -m pip install -r requirements-pip2.txt`
174142

175-
- check the `config.py`
176-
- in **run.py** edit the port of the app (Default: 5000)
143+
##### To remove all pyc files :
144+
`find . -name \*.pyc -delete`
145+
or for windows users `del /S *.pyc`
177146

147+
---------------------------------------------------------------------------------------------------------
148+
-------------- SECTION PRODUCTION ENVIRONMENT SETTING
149+
---------------------------------------------------------------------------------------------------------
178150

179-
##### Customize templates edit `/app/templates/base.html`:
151+
##### Extra configs for your server production environment : ./utils
152+
with nginx, gunicorn, tornado and supervisor (upstart or supervisord) configuration files
180153

181-
> <!DOCTYPE html>
182-
> {% set bootstrap_version = '3.3.4' %}
183-
> {% set jquery_version = '2.1.3' %}
184-
> {% set modernizer_version = '2.8.3' %}
185-
> {% set bootswatch_version = '3.3.2' %}
186-
> {% set bootswatch_theme = 'slate' %}
154+
#### PRODUCTION CONFIG with GUNICORN : Use it for production with GUNICORN Upstart script and NGINX config from utils directory :
187155

188-
In case you don't like the "slate" theme, you can chose a nice theme from http://bootswatch.com/ and just replace the theme name
156+
- set environment to production with `export FLASK_CONFIG=production`
157+
or on Windows systems shell script `set FLASK_CONFIG=production`
158+
- active your Python virtual environment
159+
- GUNICORN : on shell command type `gunicorn --bind 0.0.0.0:5000 run:app`
160+
'run' is the name of your main application file 'run.py' which serve as the entry point for your application
161+
'app' is here the name of your defined application in app/__init__.py
162+
- If you visit your server's domain name or IP address with :5000 appended to the end in your web browser, you should see the homepage of your application
163+
- Create an Upstart script which will allow server to automatically start Gunicorn and serve our Flask application whenever the server boots :
164+
- create Gunicorn Upstart script : `sudo nano /etc/init/myproject_gunicorn.conf`
165+
or customize Upstart script file (edit example in file 'myproject_gunicorn.conf' in utils directory and replace 'myproject' paths by your project path) (replace user keyword by your chosen server user name ) and place it in /etc/init/myproject_gunicorn.conf
166+
- then type `sudo start myproject_gunicorn` (replace myproject with your application folder name)
167+
- After editing a code, you can refresh app by typing `sudo service myproject_gunicorn restart`
168+
- NOTE : with our Gunicorn config upstart script file, you can generate Python and Gunicorn error logs file and access logs file in your project folder 'myproject/logs'
189169

190-
##### Customize database :
191-
# Edit sql file in data folder
170+
- NGINX: then create a new server block configuration file in Nginx's sites-available directory.
171+
`sudo nano /etc/nginx/sites-available/myproject`
172+
- then customize it : see example in file 'myproject_nginx.txt' in utils directory
173+
- Then enable the Nginx server block configuration, link the file to the sites-enabled directory:
174+
`sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled`
175+
- test for syntax errors by typing: `sudo nginx -t`
176+
- restart the Nginx process to read the our new config: `sudo service nginx restart`
192177

178+
#### PRODUCTION CONFIG with TORNADO : Use it for production with TORNADO SERVER, SUPERVISOR Upstart script and NGINX config from utils directory :
179+
- todo :
180+
Supervisor Upstart config script
181+
Tornado server config script
182+
Nginx server config script
183+
Redis server-side session config script
193184

194-
##### authorization control acces with Flask-login :
195-
- in template, use current_user : {% if current_user.is_authenticated %} ... {% else %} ... {% endif %}
196-
- in controllers route, use `@login_required` to check if user is already login then `current_user` to check his role
197-
from flask_login import login_required, current_user
198-
@auth_page.route('/dashboard')
199-
@login_required
200-
def dashboard():
201-
# prevent non-admin roles from accessing the page
202-
if not(current_user.is_admin):
203-
abort(403)
204-
return render_template('auth/dashboard.html')
205185

206-
##### To install a new package and save it on requirement file:
207-
`python -m pip install <new_package> && pip list > requirements.txt && pip list --format=freeze > requirements-pip2.txt`
186+
#### CORS CONFIG FOR FRONTEND WEB APP :
187+
- edit origins url in "register cors" section in __init__.py file :
188+
CORS(app, resources={r"/*": {"origins": "http://localhost:3000"}})
208189

209-
##### To install all packages:
210-
`pip install -r requirements.txt` or `python -m pip install -r requirements-pip2.txt`
211190

212-
##### To remove all pyc files :
213-
`find . -name \*.pyc -delete`
214-
or for windows users `del /S *.pyc`
191+
### REDIS CONFIG FOR SERVER-SIDE SESSION WITH FLASK SESSION :
192+
- Install, configure and secure Redis in your server : https://redis.io/
193+
- Add in your config.py file or prod-config.py file :
194+
import redis
195+
...
196+
SESSION_TYPE = 'redis'
197+
SESSION_REDIS = redis.from_url('127.0.0.1:6379')
215198

216199

217-
##### Extra configs for your server production environment : ./utils
200+
---------------------------------------------------------------------------------------------------------
201+
-------------- SECTION SQL DATABASE
202+
---------------------------------------------------------------------------------------------------------
218203

219-
- a supervisord.conf [supervisor is used to monitor the web application and restart it, also starts the app in case you restart your server]
220-
-----------------------------------------------------------------------------------------
221-
NGINX CONFIGURATION
222-
nano /etc/nginx/sites-enabled/default
223-
service nginx start
204+
##### Customize database :
205+
# Edit sql file in data folder
224206

225-
- a simple nginx.conf
226-
-----------------------------------------------------------------------------------------
227-
SUPERVISOR CONFIGURATION
228-
http://supervisord.org/configuration.html
207+
#### COMMON SQL SCRIPT ON DATABASE FROM MYSQL SERVER :
208+
- Signin in first with `mysql -u root -p`
209+
- Create a database : `CREATE DATABASE my_database;`
210+
- Show all databases : `SHOW DATABASES;`
211+
- Select my database : `USE my_database;`
212+
- Execute script on my database, for example insert data into a mysql table :
213+
`INSERT INTO table_name (field1, field2, ...) VALUES (value1, value2, ...);`
214+
- Import and execute sql script file :
215+
`source /home/myproject_path/data/schema_mysql.sql;`
216+
- View current database : `SELECT database();`
217+
- Delete my database : `DROP DATABASE my_database;`
229218

230-
nano /etc/supervisor/supervisord.conf
231-
service supervisor restart
232219

233-
- UNCOMMENT YOUR SETTING
234-
-----------------------------------------------------------------------------------------
235-
- after you go into production, uncomment the settings from run.py for the best performance
220+
#### IMPORT SQL SCHEMA DATABASE SAMPLE IN MYSQL:
221+
`mysql -u root -p DB_NAME < /home/myproject_path/data/schema_mysql.sql`
236222

237-
Your Feedback is appreciated :)
223+
#### EXPORT SQL SCHEMA DATABASE FROM MYSQL:
224+
`mysqldump -u root -p --databases DB_NAME > /home/myproject_path/data/schema_mysql.sql`
238225

239226

240227

228+
---------------------------------------------------------------------------------------------------------
229+
-------------- SECTION TROUBLESHOOTS
230+
---------------------------------------------------------------------------------------------------------
231+
241232
##### TROUBLESHOOTS FOR BEGINNERS :
242233

243234
- READ FIRST : About Python 2 and 3 compatibility
@@ -251,7 +242,7 @@ Python Flask WEB API DEMO
251242

252243
- Error parsing in requirements.txt ?
253244
Run instead this compatible formatted file :
254-
python -m pip install -r requirements-pip2.txt
245+
python -m pip install -r requirements-pip2.txt
255246
or convert first your requirements.txt to a python 2 pip2 compatible format with this command :
256247
python -m pip list --format=freeze > requirements.txt
257248

@@ -268,18 +259,20 @@ Python Flask WEB API DEMO
268259
download and install microsoft visual c++ compiler for python 2.7 here : https://www.microsoft.com/en-us/download/details.aspx?id=44266
269260

270261
-----------------------------------------------------------------------------------------
271-
- Error Tornado module not found ?
272-
it is a problem when installing modules like tornado in a multiple python interpreters environment
273-
So run instead this common command python which precize by default the python 2 version
262+
- Error Tornado module not found ?
263+
it is a problem when installing modules like tornado in a multiple python interpreters environment
264+
So run instead this common command python which precize by default the python 2 version
274265
python -m pip install -r requirements-pip2.txt
275266
or python -m pip install ...
276267
or python run.py
277-
268+
278269
- Error Install MySQL-python on Windows systems not working ?
279270
go over to oracle, and download the MySQL Connector C 6.0.2 and do the typical install.
280271
http://dev.mysql.com/downloads/connector/c/6.0.html#downloads
281272

282273

274+
Your Feedback is appreciated :)
275+
283276
##### License: Apache 2.0
284277

285278
~~~~
@@ -289,7 +282,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
289282
you may not use this file except in compliance with the License.
290283
You may obtain a copy of the License at
291284
292-
http://www.apache.org/licenses/LICENSE-2.0
285+
http://www.apache.org/licenses/LICENSE-2.0
293286
294287
Unless required by applicable law or agreed to in writing, software
295288
distributed under the License is distributed on an "AS IS" BASIS,

0 commit comments

Comments
 (0)