DDSh

SQLite, Python, and Flask

Week 2 Overview

Day 8 – 11 – Flask (static files, databases)
I started working through learning static files. CSS, Javascript, and images. To be honest, nothing to see here folks, move along.
Databases. I love messing with databases. I always have and I am not sure why. In no way would I desire to be a DBA, at least I don’t think I would, but they are really fun to play with. Setting up the database is a little different, I cannot remember for the life of me if its different that Django, but from my recollection it seems quite similar. I will have to work on this some more.
 
 
Static files

Static files are well, static. These files include javascript, css, and images. These can all be accessed by using the {{ url_for() }} with jinja.

Super easy.

Static snippet using an image

You can then pull them directly from the dir in the (in my case) the index.html file. Neato folks!

Databases
I will again reiterate, I LOVE databases. I just find the communication between application and the ‘brain’ (so to speak) so much fun. I also think other peoples versions of fun are much different than my own.
With Flask, you have to go about the database in a strange way in my opinion. I am not saying its hard, I just think its weird. Heck, maybe it makes it easier. I have used Mongo database with Flask a long time ago, I had super fun with Mongo. Anyway, we used SQLite and SQLAlchemy for this little learning session.
 

In order for the database to work, we are going to need to update our app.py file (this is a tut on keeping a log of your ‘friends’, hence the name. I am going to build a proper useable application in my next run for all this to stick):

and we are going to initialize our database using SQLAlchemy like this:

Then I created a model for the entries:
 
Then you run ‘flask run’ in your Terminal and your db is created with the columns that we created in our model. 🙂 Nice!
At this point I needed to create ‘friends.html. (I am doing this at the end of my work on this, so it will contain a lot of information I just skimmed over)

And we have an ‘update.html’ page that uses update section in the code above to redirect to.
 

 

And there we go folks. A complete CRUD application.
 
 
 
Comments
I used the rest of the week to work on my website and start a project I am working on. Today, I did a little bit of working on fleshing out a ‘typical’ Flask setup. I usually work like this, I frankly blame in on my background for loving to lean up processes (regardless, it works). I plan to go back through this and hammer it out to a final product. I will probably eventually have this in multiple sections for different things. An ‘SOP’ for getting Flask rolling quickly. By following this process, it should make it quicker to learn as well. I will eventually set up a tutorial page on this site and make things like this available to everyone.
 

Conclusion

I really enjoyed doing this, much more than I thought I would actually.
My next step is to create an application for my work that shows a dashboard of what is going on as far as job timelines go, what roadblocks were hit, how long it has been behind, etc. I am a project manager and feel like this would help out my company. Since I am the person tracking the data, this should make all of it much easier to do.
 
This week:

 

Share this post

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest