Friday, July 22, 2016

Flask Forms

So I missed a post last week. It's not entirely due to Pokemon Go but I can't say it isn't partially to blame. I grew up playing Pokemon, so in exchange for missing a blog post I was able to indulge in a little nostalgia.

I have however been able to learn some awesome things for web development!

I started the course on Django on Treehouse and so far it has been very interesting. Previously I was using MAMP when learning PHP and MySQL. To start the server on my local machine I opened MAMP and clicked start servers which is perfectly fine. Before I go further let me pause and say that I currently took a break from Django and detoured to developing with Flask, which I will discuss in this post, because it seemed like what I would learn in Flask would be a good basis for Django. I'm not sure of the exact differences between Django and Flask yet (meaning the inner working differences, it's obvious what the differences are when writing the code), so I will mostly discuss what I learned in Flask compared to PHP. Back to starting servers, the command is written into the Python code and started with the Terminal (on a Mac) with the command (on my machine) "python3 file_name.py".

I'm sure there are other uses for Flask but so far I've only learned how to write forms. I must say I really like the format. Each form is written as a class with each field in the form being a variable in the class. Each variable/form field is given properties such as the kind of data, i.e. password or string, and other requirements the field must meet, i.e. minimum length, match a regular expression. In turn this class/form is referenced in a function in the main app file, decorated with the url route, and the function can handle the data entered into the form, such as make a query to a database.

I really like this setup. I do appreciate the fact I learned SQL from my one book. Learning the terminology to manually create and edit databases provides a good foundation for understanding how databases work. Without that knowledge I think learning Flask first could have been a crutch that would have made SQL structure more difficult to learn later. When I was learning PHP I had to write all my SQL queries out in the code. Python's Flask makes the queries for me.

What else I like about Python in web development is the syntax. In PHP I had to write much of the HTML inside PHP, which could be confusing especially when using the text editor I was using. At least that is how the book taught. But with Python I can write an HTML page and write Python code into it with {{ }} brackets. It is also nice to write a template and just swap out content with whatever is needed on a particular page.

I never understood what a web app was but now I do. What is displayed on a webpage is controlled by the program so it functions as an app, not just a static page, and can be interacted with more easily.

Currently I am using SQLite for my database in my little web app I'm writing with Treehouse videos. When writing my own I would like to use a better(more competent?) database such as MySQL instead of SQLite although I'm not sure the differences between them. I still have to figure out how to use MySQL with Python, but it can't be that much different right?

25 words or less:
Making a Python app with Flask seems very smooth but having a knowledge of raw SQL queries is invaluable when handling data.

No comments:

Post a Comment