Chapter 9
docstrings = {name.lower(): obj.__doc__ for name, obj in objs if
name[0].islower() and hasattr(obj, '__name__')}
@app.route('/')
def index():
link_template = '<a href="/functions/{}">{}</a></br>'
links = []
for func in sorted(docstrings):
link = link_template.format(func, func)
links.append(link)
links_output = '\n'.join(links)
return '<h1>Python builtins docstrings</h1>\n' + links_output
@app.route('/functions/<func_name>')
def show_docstring(func_name):
func_name = func_name.lower()
if func_name in docstrings:
output = '<h1>{}</h1>\n'.format(func_name)
output += '<pre>{}</pre>'.format(docstrings[func_name])
return output
else:
abort(404)
if __name__ == '__main__':
app.run()
This code can be found in this book's source code download for this chapter within
the 1-init folder.
Flask includes a development web server, so to try out our application all we need to
do is run the following command:
$ python3.4 tinyflaskapp.py
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat
We can see that the Flask server tells us the IP address and port it's listening
on. Connect to the URL it displays (in the preceding example this is
http://127.0.0.1:5000/)) now in a web browser, and you should see a
page with a list of Python built-in functions. Clicking on one should display
a page showing the function name and its docstring.
If you want to run the server on another interface or port, you can change the
app.run() call, for example, to app.run(host='0.0.0.0', port=5001).