If you need to know which Flask routes are configured you can execute the following command.
print flask_app.url_map
If you need to know which Flask routes are configured you can execute the following command.
print flask_app.url_map
I was busy debugging a rest services I’m building in Flask and Flask-Restplus. I needed a way to log the request and response. I found some code that can be used as a wrapper of the WSGI application that does just this.
Create a file called dubug.py that contains the following code.
import pprint class RequestLoggingWrapper(object): def __init__(self, app): self._app = app def __call__(self, environ, resp): errorlog = environ['wsgi.errors'] pprint.pprint(('REQUEST', environ), stream=errorlog) def log_response(status, headers, *args): pprint.pprint(('RESPONSE', status, headers), stream=errorlog) return resp(status, headers, *args) return self._app(environ, log_response)
This wrapper works independently from Flask and wraps the WSGI application. It shows exactly what request is going in and what response is going out.
When running Flask with the built-in server you can use it as follows.
from debug import RequestLoggingWrapper
if __name__ == '__main__':
app.wsgi_app = RequestLoggingWrapper(app.wsgi_app)
app.run()
The output goes to the wgi.error stream. For the built Flask server it is printed to stderr.
I needed to debug the Flask request. After googeling around for a while I ran into a cool trick on Stack Overflow using the pprint
module. The pprint
module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.
import pprint str = pprint.pformat(request.environ, depth=5)
This same trick can be used with all the Flask variables.
request.args
: the key/value pairs in the URL query stringrequest.form
: the key/value pairs in the body, as sent by a HTML POST formrequest.files
: the files in the body, which Flask keeps separate from form
request.values
: combined args
and form
, preferring args
if keys overlapThis is a cheatsheet for virtualwrapper.
Install virtualenvwrapper
pip install virtualenv pip install virtualenvwrapper-win
Set environment variable
Add an environment variable WORKON_HOME to specify the path to store environments. By default, this is %USERPROFILE%\Envs
. I have set it to the following.
WORKON_HOME=%PYTHON27%\env
Main commands
mkvirtualenv <name>
lsvirtualenv
workon <name>
cdvirtualenv
deactivate
add2virtualenv <full or relative path>
setprojectdir <full or relative path>
cdproject
cdsitepackages
lssitepackages
I got the following error after creating a virtualenv using virtualenvwrapper-win when running pip.
ImportError: No module named urllib3
I fixed it by adding DLLs to PYTHONPATH. This is my PATHPATH.
PYTHONPATH=%PYTHONHOME%\lib;%PYTHONHOME%\DLLs
I have been messing around with Python these last few weeks. Playing with Python means installing a lot of packages. I did this without using virtualenv
, so my default Python environment had a lot of packages installed globally.
I now have a couple of projects that I want to work on and I what each project to have its own clean environment. That means using virtualenv
. When running virtualenv
without with the option --no-site-packages
all the packages that are installed globally are included in the virtualenv
.
I wanted to remove all the global packages to have a clean default environment. I did this by running the following command.
Windows
pip freeze > remove.txt && pip uninstall -y -r remove.txt && del remove.txt
Linux
pip freeze > remove.txt && pip uninstall -y -r remove.txt && rm remove.txt
After cleaning up the default envrionment by removing all the global packages don’t forget to install virtualenv
en virtualenvwrapper-win
by running the following command.
pip install virtualenvwrapper-win
For quick instructions of the main commands please check out virtualenvwrapper-win
Github page.
I’m back… It has been a while since my last post. To busy doing other things and not keeping up with my techie skills. I have decided it is time for some new projects. I have a few ideas lined up. Python, Flask, Angular 2 and Ionic 2 are my tools of choice. So I will be blogging about my adventures the comings weeks, months, …
Okay, this blog is going to be on setting up Python on Windows. Just some quick for myself with notes for later reference.
pip install virtualenv
pip install virtualenvwrapper-win
pip freeze > requirements.txt
requirements.txt
file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using “pip list”. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:
pip install -r requirements.txt
pip install flask-debugtoolbar
pip install initpy