Flask

Flask is a lightweight framework to make Web Applications using Python (link). For many of the different applications that are built to interact with ACI, NX-OS a small framework does wonders because it stays out of the way. It's not like an application that is developed to look at the MAC address table of a NX-OS device is going to be hit by millions of requests/second. For this reason the small footprint and simple functionality of Flask make it a very nice alternative to larger application frameworks like Django. For a more extensive documentation on Flask head to the official documentation source..

When developing an application in Flask it's important to think of how the application is structured to run in your development environment and then also in a production environment. Flask makes it possible to structure the application such that it has separate running points. These running points make it possible to easily deploy the application to a webserver running apache with WSGI or NGINX using uWSGI ( or others ).

Install Flask

If you haven't read about virtual environment in Python or settings up a development environment, I highly suggest that you head over and read our Virtual Environment document and our Development Environment document. The rest of this document is going to make assumptions that are detailed on these documents.

You really want to run Flask on your computer in a virtual environment running in user space. The integration with PyCharm simplifies your work and we will show you how to create an application framework that works on your computer and is easily transferable to a web server that is running WSGI in apache.

Let's create the virtual environment and project structure.

user@learnpy.cisco.com:

mkproject myfirstflask

Once this is completed, your prompt should include ( myfirstflask ) indicating that you are working inside a virtual environment. You can can install flask inside the virtual-env.

user@learnpy.cisco.com:
pip install flask

If you execute the command pip freeze, you will end up with the list of python packages that are installed inside this virtual-env. It should be something like:

$ pip freeze
Flask==0.10.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.10.4
wheel==0.24.0

With this you can install start a simple flask application to validate that flask is operating correctly on your computer. Create a file called myfirstflask.py that contains the following:

user@learnpy.cisco.com:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

At this point you can now execute the file as python myfirstflask.py. This should output the following:

(myfirstflask)$ python myfirstflask.py
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

If you point your browser to that URL you should see that the web application is running.

Understanding Flask

This basic application that has been created provides a starting point in understanding how Flask works. After importing the flask class, the class is instantiated with the default parameter of __name__. For now we will skip explaining this in detail as it pertains to how we view Flask ( as a module or as a standalone application ).

The most important part of this is going to be the decorator @app.route('/'). This decorator is what will relate URL's to specific code to be executed. For example, we can expand the application with.

user@learnpy.cisco.com:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/help')
def hello_world():
    return 'We are here to help you with that problem!'

@app.route('/macaddress')
def hello_world():
    return 'Enter the <strong>MAC</strong> address to search in our database'

if __name__ == '__main__':
    app.run()

If you execute this new code you can then point your browser to each of these new URL's and get that specific response back from the internal web server that is running. These routes will direct each request that you want the application to take to specific code in Flask.