This application is designed as a basic introduction to Flask, Bootstrap, and Cisco Spark API's. You will be building a small website that looks like this:
The code in this repository is a starting point for the application.
- Fork this repo
- Clone the repo to your development machine.
- Create a virutalenv and install requirements
cd spark-room-viewer
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
- Add SPARK_URL and SPARK_TOKEN to your local environment. You can get your spark token at https://developer.ciscospark.com/
export SPARK_URL=https://api.ciscospark.com/v1
export SPARK_TOKEN=<YOUR SPARK TOKEN>
- Start the main application
python app.py
At this point you should have a basic webserver up and running at http://127.0.0.1:5000/
Once our basic website is up, let's start adding additional functionality, as well as making it prettier!!
Flask is a microframework for web development in Python. A simple webserver application can be defined as simply as:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
For our application we will be building upon this framework.
Jinja is a templating engine for python and allows us several key features. Flask uses Jinja as the default templating engine when generating html pages that are returned to the user. A working knowledge of Jinja is useful in other environments such as Ansible
Jinja features include:
- Dynamic content generation
- Template inheritence - maximize code re-use, and providing consistent layout across multiple pages
For more information see here
For this lab exercise, a base template has been provided in base.html we will use this to make our landing page a little bit sexier! The base template also includes all of the CSS / Javascript you will need for the remainder of this lab.
- Create a new file in the templates directory called index.html and add the following code to it
{% extends 'base.html' %}
{% block body %}
{{ text }}
{% endblock %}
This is creating a new page, based off of base.html, whose body will contain the context data text which will be passed to the template upon rendering.
- Modify the URL route to use the template.
Change the following lines:
@app.route('/')
def hello():
return "Hello World!"
to
@app.route('/')
def hello():
return render_template('index.html', text="Hello World!")
-
Refresh the page. you should now see a "jumbotron" box at the top of the page, containing some additional information about the app we are building.
-
Explore the base.html template to see where the magic comes from.
The Spark Developers site is a great resource for exploring the Spark API's. For our application we are intersted in getting a list of spark rooms.
For this excercise, you will complete the get_rooms function such that it contacts the Spark API, and returns a list of rooms. To test your code, simply press the refresh button on the rooms web page.
Add a column in the web page that will display when the last activity in the room was
Add another column that will display the number of members in each room.
Make the table sortable by any of the columns