Debugging Python with GDB
To run python under gdb there are also two ways.
Interactive:
$ gdb python ... (gdb) run <programname>.py <arguments>
Automatic:
$ gdb -ex r --args python <programname>.py <arguments>
If the process is already running, you can attach to it provided you know the process ID.
$ gdb python <pid of running process>
Gevent with debug support for Flask
from gevent.wsgi import WSGIServer from werkzeug.serving import run_with_reloader from werkzeug.debug import DebuggedApplication app = Flask(__name__) @run_with_reloader def run_server(): http_server = WSGIServer(('', 5000), DebuggedApplication(app)) http_server.serve_forever() run_server()
References
https://coderwall.com/p/q2mrbw/gevent-with-debug-support-for-flask
Deploy Flask using Gevent
from gevent.wsgi import WSGIServer from yourapplication import app http_server = WSGIServer(('', 5000), app) http_server.serve_forever()
References
http://flask.pocoo.org/docs/1.0/deploying/wsgi-standalone/
Make Flask externally visible on PyCharm
Additional options
--host=0.0.0.0 --port=1883
References
https://www.jetbrains.com/help/pycharm/run-debug-configuration-flask-server.html
Persian Text in Matplolib
from bidi.algorithm import get_display import arabic_reshaper
def make_farsi_text(x): reshaped_text = arabic_reshaper.reshape(x) farsi_text = get_display(reshaped_text) return farsi_text
xlabel = "یک محور همینجوری" ylabel = "یک محور دیگه همینجوری" title = "یک نمودار همینجوری" plt.scatter(X[:,0],X[:,1],c = y, cmap = 'spring') plt.xlabel(make_farsi_text(xlabel),fontsize = 15) plt.ylabel(make_farsi_text(ylabel),fontsize = 15) plt.title(make_farsi_text(title) , fontsize = 20) plt.show()
font_title = {'family': 'B Farnaz', 'color': 'red', 'weight': 'normal', 'size': 30, } font_labels = {'family': 'B Nazanin', 'color': 'black', 'weight': 'normal', 'size': 20, } plt.scatter(X[:,0],X[:,1],c = y, cmap = 'spring') plt.xlabel(make_farsi_text(xlabel),fontdict = font_labels) plt.ylabel(make_farsi_text(ylabel),fontdict = font_labels) plt.title(make_farsi_text(title) ,fontdict = font_title) plt.show()
References
http://imuhammad.ir/2017/09/23/farsi-plots-python/
https://pypi.org/project/python-bidi/
https://github.com/mpcabd/python-arabic-reshaper
How to get data received in Flask request
from flask import request
For URL Query parameter, use request.args
search = request.args.get("search") page = request.args.get("page")
For Form input, use request.form
email = request.form.get('email') password = request.form.get('password')
For data type application/json, use request.data
# data in string format and you have to parse into dictionary data = request.data dataDict = json.loads(data)
References
https://stackoverflow.com/questions/10434599/how-to-get-data-received-in-flask-request
Best Practice for virtualenv and git repo
pip freeze > requirements.txt
References
http://libzx.so/main/learning/2016/03/13/best-practice-for-virtualenv-and-git-repos.html
HTTP requests and JSON parsing in Python
import requests url = 'http://maps.googleapis.com/maps/api/directions/json' params = dict( origin='Chicago,IL', destination='Los+Angeles,CA', waypoints='Joplin,MO|Oklahoma+City,OK', sensor='false' ) resp = requests.get(url=url, params=params) data = resp.json()
References
https://stackoverflow.com/questions/6386308/http-requests-and-json-parsing-in-python