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.
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)
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.
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 string
request.form: the key/value pairs in the body, as sent by a HTML POST form
request.files: the files in the body, which Flask keeps separate from
args if keys overlap
This is a cheatsheet for virtualwrapper.
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.
add2virtualenv <full or relative path>
setprojectdir <full or relative path>
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.
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
I wanted to remove all the global packages to have a clean default environment. I did this by running the following command.
pip freeze > remove.txt && pip uninstall -y -r remove.txt && del remove.txt
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
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 have a large number of blog posts and a few of them are quit long. The WordPress theme I am using show the complete post on the home page. This means users visiting the home page need to scroll “forever” going through the posts. This was annoying me. I was thinking of switching to a theme that limited the blog post size on the home page providing a read more link to the full blog post. I could not find a suitable theme that worked well for desktop, tablet and smartphone. What to do? Continue reading