I’ve been traveling lately so haven’t been able to write as regularly as I would like to. I’m currently in Chiang Mai and have also spent some time in Seoul and Bangkok. It’s been a lot of fun. I’m traveling with my girlfriend. We’re trying to keep a nice balance between relaxing and doing touristy stuff. I started on a new project and plan to keep coding during the trip.
I’m going to show you how to make a simple Flask app.
You first go to the folder of your project.
Next, you create a virtual environment. In this case, I’m naming the virtual environment “virtual”. Virtual environments let you install python specifically for a project letting you use whatever configuration or version you need.
Then, I install flask. I’m using the pip in “virtual\scripts” to make sure I’m installing flask to the python in my virtual environment.
#! python3 # simpleapp.py from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello' app.run()
I create a “simpleapp.py” file and added it to my project folder. In line 6, an app object is instantiated from the Flask class. The decorator invocation line registers a view function for a given URL rule. The function is home in this case. The last line runs the app.
Finally, we can get our app running by executing the “simpleapp.py” file through the python in our virtual environment. By default, this runs the app on the localhost.
I’m currently trying to figure out how to make an app that can manage users. I was previously using sqlite, but I wanted to use PostgreSQL for Heroku.
I found the video below really helpful in getting started with PostgreSQL.
I received some advice from a friend who worked as a software engineer and consultant. He recommended focusing on theory since technologies are constantly changing. Try to get a general understanding of why things are working.
Eat a light lunch
Don’t eat anything else
Go on a 3 hour hike at 5PM
I’ve come across decorators while using Flask and this is an attempt to better understand how they work.
Use of decorators can appear with an @ sign before a function definition. This line itself isn’t the decorator, but invokes the decorator.
A decorator is a function that takes a function in as an argument and returns a function.
Here is an example below.
def function1(): print('hello') def function2(func): def function3() : print('add something before') func() print('add something after') return function3 function1 = function2(function1) function1()
In line 12, we see that function2 is modifying function1. Function1 originally just prints ‘hello’. If you call function1 in line 13, it will print ‘add something before’, ‘hello’, and ‘add something after’. Function2 is the decorator here.
def function2(func): def function3() : print('add something before') func() print('add something after') return function3 @function2 def function1(): print('hello') function1()
We can rewrite the previous line 12, with what we have in line 8 here. This acts in the same way and results in the same outcome. The @function2 is the decorator invocation line.
Some other resources: