geek, traveler, writer, hacker
A couple years ago, I started playing around with Python. I had wanted to learn to code for awhile -- generally, I know enough to be dangerous, but that's about it -- but being at a job that I was unhappy with forced me to finally take it seriously. I dabbled with Learn Python the Hard Way and Codecademy, but always kind of got... stuck. I'd forget things, or not work on it for a couple of weeks, or just not feel very motivated aside from my desire to find a new job. The problem, of course, is that I learn much better when working on a project and trying to tackle practical problems that I actually care about.
About a year and a half ago I got a new job that I love, one that is much more flexible and much more technical, and has resulted in me learning a lot about networking, Cisco, Linux, and because I hate doing the same thing over and over again, automation. That last point -- automation -- was key. After some waffling, I started to seriously explore Ansible, because I discovered that if nothing else, I could use it to generate configuration templates and do IP calculation that I'd been doing manually. That catapulted me from crawling to taking my first real, albeit stumbling, steps.
Since then I've been pretty busy with a lot of little projects, finding excuses to Ansible-ize different things (like my WIP Raspberry Pi console server) and helping out with other nerdy projects that pop up.
I've actually been wanting to play around with Flask for awhile, and finally had the opportunity when I needed to re-do the SFMIX site (which, as of writing this, is not yet live... but it should be sometime in May). Flask is a Python driven micro framework that uses the Jinja2 templating system... which Ansible does too, actually.
And since I already knew a little Python, and had some experience with Jinja thanks to Ansible, it's been a lot easier than I expected. I've not just been working on the SFMIX site, but also on this site (now powered by Flask!) and some other projects too.
So, what's the fuss over Flask? It's actually dead simple once you wrap your brain around it. Here's an example directory and file structure:
/mysite /static /js /css /img /templates base.html index.html about.html mysite.py requirements.txt __init__.py
static and any of your webpages you want to display should go in
templates. The beauty of Jinja and the
templates directory is that you can extend templates, meaning you can have your header, footer, etc., in one single file. For example,
base.html is like your empty apartment, while
index.html is your living room couch and
about.html is your bed.
It's really neat.
Your site "lives" in the
mysite.py -- that's where you import any Python packages you need (like Flask), map URLs to html files, etc. This can get pretty complicated, because you can do a lot of really cool stuff here, but it can also be pretty straightforward. For example, here's a basic Hello World app from the Flask website:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
Finally, you might be wondering about
requirements.txt and the init file. The
requirements.txt file makes it easy to install multiple packages via pip by just listing them in there, then running
pip install -r requirements.txt. When you start doing more complicated Flask apps, you can easily end up with a lot of packages you need to install for it to run.
The init file isn't something you need to worry about when just developing a Flask app on your computer, but you do need it if you want to run a Flask website. It basically tells Python "Hey, these directories have packages!"
I did figure out some good tips and tricks with Flask and the Jinja templating, so I'll try to get around to writing about those in the future. But for now... this isn't a bad start.