Attempting to Learn Flask, Part 3

Attempting to Learn Flask, Part 3

I should close the loop on this series. Part 1 and Part 2, if you’re interested.

The conclusion is, I haven’t mastered it, but I’ve definitely figured out the basics. I thought I’d need to learn more from CS350 for some time before getting here. Instead I found myself going back to just two of the videos in Charles Severance’s “Using Databases with Python” several times and jogging memory with code I’d worked with during that course. I’m forgetful so it was great to have his videos on hand. I also spent a lot of time googling, reading, and YouTubing tutorials.

Basically, I had to draw from everywhere at this point, and the range was really, really wide.

The happy news is, I can now:

  • Use Python to do magic on the back end
  • Use Flask to deliver magic to the front end
  • Use SQLite3 and/or PostgreSQL to handle data (Create, Read, Update, Delete)
  • Doll up my web app a little bit with Bootstrap

I’ve made a couple of apps already:

One uses Python to concatenate randomly selected strings from various lists to form grammatically correct English sentences and then displays a new one in the middle of a webpage at regular intervals.

The other draws from the IMDB database, banishes all USA and Western European films, randomly picks 10 from the rest of the world, and displays them along with their trailers from YouTube. This one was not so trivial for me. There was a lot of junk data in the IMDB database to clean up, and I had to work out how to scrape YouTube to embed the trailers on my site.

So, as of early 2017, I can verify that my proposed path of least resistance for learning how to build a web app from scratch is a pretty decent strategy.

I can’t say if it’ll continue this way. Already there are certain things that are falling into doubt. For instance, CS350 leverages AngularJS a little bit to do Flask stuff, and in the course of reading all the other sources I found out that there is a lot of uncertainty surrounding the future of AngularJS right now. That particular case is not terribly important. Just a small example. We can use other JS frameworks anyway.

What’s next for me? I’m going to build up that film site of mine. Add users to it, work on some formulae running in the back end. Get more familiar with PostgreSQL. Get a better handle on how to mash Flask up with Bootstrap. And then maybe learn Cordova next year to build mobile apps.

Coding is great fun, especially since I use it to engage real world problems. For example, the winner-takes-all effect. Films from USA and Western Europe, and especially from Hollywood, saturate almost every single one of our media environments. My film site bans all USA and Western European films in order to allow the diversity of the rest of the world’s film heritage to come to the fore. Turns out, it’s wonderful. Using it, I discovered amazing, beautiful films from Azerbaijan, Indonesia, Malaysia, Turkey, and elsewhere. I think there was even one from Antigua and Barbados. It’s not ready for the public yet, but I hope to put it up somewhere just for show-and-tell soon.

Another big reason that drove my learning was the oppressive pecuniary pressures in the art world. I wonder if I’m ready for an entry level/junior position yet. I have no idea. Time to try looking around for a job now as well, I guess. At the very least, I’ll get a better idea of what the world wants.

How and Where to Take a (PostgreSQL) Dump, or, When Documentation Lacks Detail

How and Where to Take a (PostgreSQL) Dump, or, When Documentation Lacks Detail

I’ve made crazy progress in the last couple of weeks. As a result of that, I’ve managed to get my grubby hands onto SQL and I’ve even wrestled with which database management system to use. I used Digital Oceans’ succinct article to settle on PostgreSQL.

Unfortunately, unlike the SQLite I’m a bit more familiar with, PostgreSQL databases (DB) don’t come in one handy portable file. I spent maybe a couple of hours poking around the PostgreSQL directory wondering where the DB was. Then I googled it and read some articles a bit more closely and figured out it’s more complicated than a single manifestation in a single locus.

So, using the vocabulary I had, I googled “how to export PostgreSQL database” and a couple of other variants related to it. Didn’t work. But at some point, I found out that you have to do a dump. That gets you a file you can move around. But not many were good at explaining it, or even the concept of it. Finally, I read the documentation here and thought, this should be easy. But once I’d done a pg_dump, I couldn’t find the file. I spent another couple of hours trying out various things.

Then, finally, finally, I YouTubed “postgresql dump” or something like that and found this silent video. I watched it carefully and realised that the dump is created in the directory from which I work in the command line. And that has not been mentioned in all the previous places I’d looked in, including the documentation. Maybe it’s obvious to experts. But beginners need something a bit more explicit.

Jinja2: A Medium Between Python and Front End

Jinja2: A Medium Between Python and Front End

I first looked at Flask and Django because people said that if I want to use Python on a webpage I’d need a web framework. Turns out, the simpler thing to learn is not a web framework but a template engine.

What is a template engine? Who the hell knows? Do you think I knew as a beginner? Why do all these tutorials call it that? Tutorials for beginners, no less. And if you want to even learn one, Jinja2 in my case, you can barely find a tutorial that focuses on it. Most Jinja2 tutorials involve a great deal of Flask.

So here’s the thing, Jinja2 is the medium that weaves between Python and a webpage on the front end. It allows us to show results from a Python function in the back end on a web page. It can’t do everything but, sometimes, it’s more than enough for us.

As far as I know, Jinja2 remains an integral part of the Holy Trinity of Flask-Jinja2-Werkzeug. Pip install Flask would get you the entire Trinity. However, you don’t need to learn all of Flask to put Python functions on the web. You do still need to import flask in your script and learn the basics of where to put your files and how to name your folders the way Flask likes it, but after that, it can be simple. Here’s an example.

In your index.html file, which must be placed in the templates folder according to Flask’s preferences, you can have a string like, say:

I like {{type_of_jam}}.

In your server script, you can do:

def mainIndex():
list = [‘butter’, ‘apple jam’, ‘orange marmalade’, ‘crude oil’]
jam = random.choice(list)
return render_template(‘index.html’, type_of_jam=jam)

And that’s it. Your string on the webpage randomly displays a type of jam.

You can do other things with it, of course, and you’ll need some more knowledge to better appreciate what it can do. But my point is, for beginners to get to here, we first need to tunnel through mountains of other stuff, a lot of which is unnecessary (even if nice) simply because the current ecology of pedagogical resources don’t design for noobs.

+1 to Digital Literacy

+1 to Digital Literacy

I stumbled into an idle moment and used it to ponder digital literacy. I’ll summarise the Wikipedia link’s summary as the abilities to:

  • “Deduce information from visuals”
  • Create remixes
  • Navigate the non-linearity of the web
  • Find info online and be critical about it
  • Not be an asshole online

I know my summary is not perfect, but neither is the original. I think mine’s more precise. I mean, “don’t be an asshole”. That’s pretty damn clear, right?

Anyway, I think we should add one more. Admittedly, it’s a high-hanging fruit, but I think it’s increasingly important:

  • Find solutions to coding problems even if they are someone else’s script on Stack Overflow that you don’t fully understand.
Programmers Protest Broken Interview Process

Programmers Protest Broken Interview Process

Someone responded to “whiteboard algorithm hazing” and a lot of other programmers followed suit. The term refers to when interviewers make you write code on a whiteboard.

It’s a great piece. Check it out. Not extensive, but super important to let beginners like us know we don’t have to be so fucking intimidated, and scared out of wits, and anxious to death all the time just because we think we aren’t good enough. That’s no way to live.

I don’t think I’d ever be able to pass a whiteboard test simply because coding is a different embodied experience from writing. I’m pretty brilliant at writing, and I can write really well on the whiteboard, but it’s not the exact same animal as coding.

Both writing and coding are thinking processes. We don’t use just our brains to think. Writing helps us to articulate and develop our ideas. At least for me, if I don’t write, I’d be stuck at or near the inception point of an idea. If I start writing it out, even if I have no idea where I’m going with it at all, not even the next step, I develop it into a stronger concept and at a much faster pace too.

Usually, I use both pen and keyboard to do it. I think some studies show that there’s a link between exercising the pen and brain activity, or something. Idk, forgot the details. But anyway, that’s why I call it “embodied”. I think other people have called it “embodied” before me.

Coding is the same, but also different. It has its own fluidity that works under certain conditions. Whiteboard hazing separates the coder from the environment that helps him best, like taking a fish out of water and asking it to swim. Maybe some fish can do it, but it’s not usually helpful. Tabbing between Google, Stack Overflow, Quora, YouTube, Coursera, documentation, and whatever else resource you have is part of the flow.

Plus, community is important to coding. Once you’re in the flow, you access other people’s work, read other people’s solutions, and ask others for help. Inevitably, you contribute too, even if it’s reminding a friend that he/she missed a semi-colon somewhere. IRC channels, forums, Facebook groups, hackerspaces, and friends are super important for that.

Ironically, the geek so stereotyped to be awkward in social circumstances really knows how to build communities and relationships that are vital to problem-solving.

I’m not an experienced coder, but I’d say it’s ok not to have everything at the tip of your fingers.

Path of Least Resistance: From Knowing Nothing to Python to Flask

Path of Least Resistance: From Knowing Nothing to Python to Flask

I have to work on urgent stuff so I can’t go on with Flask for the moment. 😦 But I want to quickly pen down what I think might be the relatively easiest way to go from knowing nada to learning to make a web app, based on my experience.

Code Academy

Easy stuff. Dip your feet in shallow waters with its Python course. Also do its HTML, CSS, and Javascript stuff.

Learn Python the Hard Way (LPTHW)

First half was good, second half got too hard, partly because the way things were explained did not agree with me. I quit this site in its last third, I think.

I think I did Python basics on this site in the same period I did LPTHW. I may have done one or two other basic tutorials too because I wanted strong enough fundamentals to make learning the harder stuff easier later.

I don’t think my fundamentals are strong even now though. For instance, I’ve never written a class and I’m not sure I know how to.

Web Design for Everybody (Basics of Web Development and Coding), with Colleen van Lent

Lent is very clear on most things. I also did a Bootstrap course somewhere online but I can’t remember what it was. Learning front end stuff helps me understand which code comes from what programming language and where it should go when doing Flask. Learning Flask will involve Python, SQL, HTML, CSS, and JS, at some point or other.

I didn’t do the Capstone for this or any other Coursera course I did, because it costs money.

Python for Everybody, with Charles Severance

The first couple of Python courses in this specialisation are pretty all right. But I feel beginners require a lot more hands-on/embodied experience before we know to use Stack Overflow and other resources as vital supplements to understanding the things instructors miss out, and to troubleshoot all kinds of problems course instructors could not predict might affect their students. This is why it’s a good idea to go through Code Academy and LPTHW before doing this Coursera specialisation.

Severance is the clearest instructor I’ve come across on the things he goes through in the “Using Python to Access Web Data” and “Using Databases with Python” courses. I gave up working on a couple of the more complicated projects towards the end, in part because the instructions grew less and less and I kind of still needed them. But most of the courses are really pretty damn great.

Templating With Jinja2 in Flask

I didn’t finish even midway through part 3 of this series but the first two parts gave me a pretty ok beginner grasp of Flask with respect to Jinja2.

CS350 Flask tutorials on YouTube

For me, the clearest instructor and content so far when it comes to Flask. I’m slightly less than halfway through at the moment, though, I think.

UPDATE 19 April 2017: Model-View-Controller (MVC) Paradigm

At this point, I feel, it’s time to focus on two things: security, and the MVC paradigm. I am presently trying to figure out flask-security, but haven’t found good enough teaching/learning material for it. Please message me if you know some.

For MVC, this is a good introduction:

It helped me a lot, made me realise I could reduce my code by around 90%.


Throughout this journey, Stack Overflow has been absolutely vital for finding solutions to miscellaneous problems. But usually, I google my problems and it finds solutions on Quora and a lot of other great websites too. I tried duckduckgo but its results were dismal for research.

UPDATE 2 April 2017: Yes, CS350 is good, but slightly more than halfway through it, I found myself building my own web apps, and that meant that I began seeking solutions from everywhere. It was a sudden and significant decentralisation of sources. A bit more detail in Part 3 of my experience learning Flask.

Attempting to Learn Flask, Part 2

Attempting to Learn Flask, Part 2

23 Feb 2017

I thought I’d break up the holy trinity of Flask to make it easier to learn: Flask, Jinja2, and Werkzeug. As it turns out, separating Flask from Jinja2 might be impossible. I don’t know for sure yet. Anyway, I used Shalabh Aggarwal’s tutorial, which is thankfully focused on templating, which means a bit more Jinja2 than usual.

I am still trying to get the hang of Flask spreading code over many files, and involving code from Flask, Jinja2, HTML, and even JS and CSS. But I feel like maybe I am progressing, even if just a tiny bit. It’s hard to tell since progress is incremental. I’ll know soon, I hope.

I couldn’t do part 3 of Aggarwal’s tutorial. Something is wrong somewhere and I have no clue what it is. In part 2, I couldn’t import render_field from _help.html as _help.jinja. So I googled it and found a solution, which was to import the whole _help.html and call render_field normally, like form.render_field(). I have no idea why Aggarwal’s method didn’t work for me.

25 Feb 2017

So, after slightly more than a full week floundering, I might have found a good tutorial. I don’t know whom he is, but this CS350 chap seems pretty ok so far, although admittedly, we haven’t reached any of the difficult stuff yet.

In spite of that, I do like that he introduced me to Cloud9 and Gitlab. Cloud9, especially, might solve some issues for me. For instance, the Werkzeug site uses Redis for its tutorials, and it doesn’t quite run on Windows, and I couldn’t figure out how to use the version ported to Windows to work alongside the code the Werkzeug tutorial provided. Cloud9 allows me to run Linux, amongst other things, which means I can use Redis if I want to. CS350 doesn’t use Redis though.

I know I could have used VirtualBox, but I find it a bit of a pain to use, for reasons I no longer remember. Haven’t used it in a while.

And yes, after I tried out Aggarwal’s Jinja2 templating tutorial, I decided to try a Werkzeug one, using the official site’s tutorial. It was horrible. I gave up midway because I couldn’t figure out the Redis bit.

26 Feb 2017, Part A

Following CS350 into PostgreSQL on Cloud9. It’s unfamiliar, because I don’t see the SQL database file anywhere in Cloud9. The method of interface makes it a bit hard to grok. The data is rendered in text on the command line. Is that ASCII? I don’t know. Anyway, I do understand it a little because a month or so ago I did Charles Severance’s excellent Using Databases with Python. It uses SQLite. So, I kind of get some of the commands. I wish I could continue with SQLite though and the DB Browser for SQLite within the CS350 tutorial though. It would make the learning experience a lot smoother.

It might be possible if I had more knowledge, but in that scenario, jumping between the YouTube channel, Cloud9, Gitlab, and whatever other resources I might leverage to use SQLite would make things more complicated.

26 Feb 2017, Part B

Ran into seemingly insurmountable problems when CS350 decided to use psycopg2 in its tutorial. Various forums had different solutions and some of it coincided with my own suspicions. Generally, these were:

  • The right dependencies hadn’t been installed.
  • I didn’t know to use sudo to install/upgrade it.
  • I didn’t know apt-get and pip well enough to use them properly.

It was super confusing. At first, I used apt-get and pip, and sudo apt-get and sudo pip, to install it. I ran pip freeze and found it wasn’t in the list of installed packages. I tried various things after that and at one point, pip freeze said I had the latest version of psycopg2, but I checked Pypi, and I definitely had the wrong one. I ran sudo apt-get update but it still gave me the wrong version.

I basically ran through permutations of solutions for all three of the above possible problems and finally,  got the right version of psycopg2. It took about two hours and every minute grew more and more despondent until that final moment it suddenly worked.

This is how I solve this kind of Python problem(s). I had to do this with NLTK too. It always drains me. Now I need to take a break.

Anyway, this problem prompted me to find out what the differences are between apt-get and pip. Now I know.

26 Feb 2017, Part C

On a tram to friend’s for dinner. Put two and two together and figured out that I can use <input type=’submit’ name=’placeholder’>request.form[‘placeholder’] and cur.execute(“””SQL COMMAND”””) to mediate between front end users and database.

Am I right? Is this the standard way to do it? Can’t wait to go on with the tutorials and/or figure it out another way.