A Guide on Flask and Azure SQL

Search for a command to run...

No comments yet. Be the first to comment.
A quick cheatsheet for TMUX commands that I can never remember. tmux yum -y install tmux Out of tmux new session tmux new new session with name tmux new -s sessionname show sessions tmux ls attach to session tmux attach-session -t 0 Go to last sessi...
Here's a fun error: "not syncing vfs unable to mount root fs on unknown block" I'm running CentOS7 on Linode and happened to run into this error on one of my systems. Quite unusual, so I did the quick and dirty method and restored a backup of my VM....
I discovered a fun git command that I wasn't aware of before. git switch -c <new-branch> Using git switch, you can take existing, uncommitted files and move them to a new branch. Say you're working in the master branch and want to move those changes...
Well, maybe not so hidden, but definitely not advertised. Vudu has an API that, for the life of me, I could not find advertised anywhere, even with an advanced Google search. So, I'll take it upon myself to scrape their webpage and dissect their ho...
I didn't realize until recently that you can run DynamoDB on a local machine on either Windows, Mac, or Linux. This obviously can be hugely benefitial for learning and dabbling in DynamoDB, without racking up a large AWS bill. Let's see if we can get...
Building off the previous blog post in which I query Azure SQL from a Linux machine using Python, we're going to apply the same principals to a Flask application.
If you're unfamiliar with flask, check out my other guide on Building a Flask App in 5 Minutes
For this project, we'll use the same database and table used in the previous example. To recap, we have a database with a table called "data" that has two columns, "name" and "quote".
We're going to build a simple flask app that displays the list of names and their quotes to a web page.
Here is the starting point of my routes.py file, now including the import of my AzureDB class.
from flask import Flask, render_template, request
from AzureDB import AzureDB
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/')
def hello():
return render_template("quotes.html")
I'm creating a new template file called quotes.html, which will look like this:
{% extends "layout.html" %}
{% block content %}
<p><em>So and so </em> once said this: <em> something </em>
</p>
{% endblock %}
Let's run it just to make sure things are all tidy on this front. Then we'll add an actual query in here.
Looks good

Now let's add some queries and some logic.
We'll use the same basic principal we used last lesson and apply it to our route, meaning we'll establish a connection with a Python "with" and get our data. Then we'll pass our data to our renderer, then apply jinja2 logic to parse the data.
First, in routes.py, add the query from our AzureDB.py class.
@app.route('/')
def hello():
with AzureDB() as a:
data = a.azureGetData()
return render_template("quotes.html", data = data)
*Hold on... what's "data = data" mean?
The first part sets data as it will be delivered to the template page. The value it is being set to is called "data" as well, which is defined on the line directly above it. *
Now open the quotes.html template. The jinja2 template will apply logic that will loop through each item in the data. If there is no data, a simple "Nothing Found" message will display.
{% extends "layout.html" %}
{% block content %}
{% for d in data %}
<p><em>{{ d.name }} </em> once said this: <em> {{ d.quote }} </em>
</p>
{% else %}
<em>Nothing Found :(</em>
{% endfor %}
{% endblock %}
Save all of these files and restart your flask app.

It's that easy!
-AJ