@@ -111,10 +111,10 @@ We can add new key-values or change values as any dictionary in python.
111
111
112
112
However, for a large project,
113
113
if there are multiple environments, each with different set of config values,
114
- we can create a configuration file. Refer to the link below for more.
115
-
116
- https://pythonise.com/series/learning-flask/flask-configuration-files
114
+ we can create a configuration file. Refer to the links below for more.
117
115
116
+ * https://pythonise.com/series/learning-flask/flask-configuration-files
117
+ * https://flask.palletsprojects.com/en/0.12.x/config/#configuring-from-files
118
118
119
119
120
120
Manipulating HTML
@@ -268,7 +268,11 @@ Below shows up to upload a file, e.g., an image to a directory in the server.
268
268
file .save(img)
269
269
270
270
return render_template(' index.html' )
271
-
271
+
272
+ To upload multiple files, end the html form tag with "multiple",
273
+ ``<form action="/upload" method="post" enctype="multipart/form-data" multiple> ``
274
+
275
+
272
276
273
277
Logging
274
278
-------
@@ -337,7 +341,7 @@ If we run ``docker ps``, under PORTS, we should be able to see
337
341
that the Docker host IP 0.0.0.0 and port 5000, is accessible to the container at port 5000.
338
342
339
343
340
- Environment Variables
344
+ Storing Keys
341
345
----------------------
342
346
343
347
We can and should set environment variables; i.e., variables stored in the OS,
@@ -362,6 +366,16 @@ To do this, in Mac/Linux, we can store the environment variable in a ``.bash_pro
362
366
echo $SECRET_KEY
363
367
364
368
369
+ We can also add this to the ``.bashrc `` file so that the variable will not be lost each time
370
+ you launch/restart the bash terminal.
371
+
372
+ .. code :: bash
373
+
374
+ if [ -f ~ /.bash_profile ]; then
375
+ . ~ /.bash_profile
376
+ fi
377
+
378
+
365
379
In the flask script, we can then obtain the variable by using the os package.
366
380
367
381
.. code :: python
@@ -378,6 +392,59 @@ container.
378
392
sudo docker run -e SECRET_KEY=$SECRET_KEY -p 5000:5000 comply
379
393
380
394
395
+ Changing Environment
396
+ --------------------
397
+
398
+ Sometimes certain configurations differ between the local development and
399
+ server production environments. We can set a condition like the below.
400
+
401
+ Note by default flask environment is set to production
402
+
403
+ .. code :: python
404
+
405
+ if os.environ[' FLASK_ENV' ] == ' production' :
406
+ UPLOAD_URL = ' url/in/production/server'
407
+ elif os.environ[' FLASK_ENV' ] == ' development'
408
+ UPLOAD_URL = ' /upload'
409
+
410
+
411
+ We can then set the flask environment in docker as the below.
412
+ Or if we are not using docker, we can ``export FLASK_ENV=development; python app.py ``.
413
+
414
+
415
+ .. code ::
416
+
417
+ # when testing in production environment, comment out below
418
+ CMD export FLASK_ENV=development
419
+
420
+ ENTRYPOINT [ "python", "-u", "app.py" ]
421
+
422
+
423
+ A more proper way to handle environments is mentioned in flask's documentation below.
424
+
425
+ * https://flask.palletsprojects.com/en/0.12.x/config/#configuring-from-files
426
+
427
+
428
+ Scaling Flask
429
+ -----------------
430
+
431
+ Flask as a server is meant for development, as it tries to remind you everytime you launch it.
432
+ One reason is because it is not built to handle multiple requests, which almost always occur in real-life.
433
+
434
+ The way to patch this deficiency is to first, set up a WSGI (web server gateway interface),
435
+ and then a web server. The former is a connector to interface the python flask app to
436
+ an established web server, which is built to handle concurrency and queues.
437
+
438
+ For WSGI, there are a number of different ones, including gunicorn, mod_wsgi, uWSGI, CherryPy, Bjoern.
439
+
440
+ For web servers, the two major ones are Apache and Nginx.
441
+
442
+ * https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
443
+ * https://medium.com/ww-tech-blog/well-do-it-live-updating-machine-learning-models-on-flask-uwsgi-with-no-downtime-9de8b5ffdff8
444
+ * https://www.appdynamics.com/blog/engineering/a-performance-analysis-of-python-wsgi-servers-part-2/
445
+ * https://www.fullstackpython.com/wsgi-servers.html
446
+
447
+
381
448
OpenAPI
382
449
---------
383
450
0 commit comments