So I’ve decided to convert (re: re-work) one of my projects using Flask. I need to re-sharpen my python skills and I’m a bit too lazy to re-learn Django (way too bloated).
So I started by following simple tutorials and I’ll keep updating interesting things I find throughout the time.
Firstly, when installing flask, I found about 30 tutorials that all start with
Install flask by doing:
pip install flask
If you’re like and using a linux distro, you most likely won’t have the pip package manage, I use apt-get at home and yum at work. So if you find any of those tutorials, you’ll probably be greeted by the jaded “You don’t have pip mwahahaha”
Long story short, here’s how you install flask on Ubuntu
sudo apt-get update -y
sudo apt-get install python-pip
I like the ‘-y’ flag because I’m too lazy to keep typing ‘yes’ when they ask if I’m ok with giving up some Kb/Mb of space. (A good developer is efficient [re: lazy haha])
sudo pip install Flask
Ensure you are the root user or you’ll get an error.
I use vim, so we’ll use that to make a minimal application for testing purposes.
from flask import Flask
app = Flask(__name__)
return “Hello World!”
if __name__ == “__main__”:
- This simple code firstly imports the Flask class.
- Next we create an instance of the class with the first parameter being the __name__. Depending on if this application is the main one or a module becomes rather important, so this is where we begin our distinction.
- Next we use route decorator to capture the actual route that we want to respond to.
We then define the function that will run once the user navigates to that page.
if __name__ == '__main__': makes sure the server only runs if the script is executed directly from the Python interpreter and not used as an imported module. (See what I said earlier). The run() takes several parameters, one being port. We set that to 8000.
By default, the flask application will run on port 5000, I changed the port here so that you can see (and know how to manipulate the port you want your application to run on. Please note that running on port 80 may present errors if you already are using apache or another server since this will be using that port number)
I’ve started doing some challenges online to hone my skills… Essentially on days when I’m not outside gyming or running around, I like to do simple coding challenges. I most recently completed a Space Invader’s challenge. I’ll be posting more as I do more.
I pasted the solution and the running code here
So I decided to give Amazon web services a try.. They’re totally awesome you should check them out. Either way, I wanted to start blogging again since I got locked out of my last blog [for lack of use and forgetting pretty much all the credentials ].
So I started setting up Amazon services and I began hosting with with S3. (More on that in a different post)
Either way, I found a great tutorial from coenraets. I’ve been using this guy for tutorial (and sometimes emotional) support since my undergrad days. The tutorial was brilliant and I wanted to recommend it since it was perfect for me and getting back up to speed in literally 5 minutes. So all you tech bloggers who are using the excuse that it’ll take to long to start back up, here comes coenraets waltzing to put a damper on your procrastination (self testimonial) .
I only have a few additions based on issues I ran into:
Firstly, when remotely accessing your linux instance via SSH, you’re system may have a user pre-determined. For Ubuntu, the user name is ubuntu. For Fedora, the user name is either fedora or ec2-user. For SUSE Linux, the user name is root. However, in order to SSH successfully into the linux instance, you’ll need to set your user to ec2-user in the session. So
ssh ec2-user@your_instance_public_dns -i security.pem
Secondly, when setting up an instance of linux (I used Basic 32-bit Amazon Linux AMI.) You may forget to set a specific set of inbound traffic rules. This may become an issue if you try to SSH in from different computers or if you try to access the test.php or even the regular default apache test page from your public DNS (re: tutorial).
Scroll across and select your security group. You should then right click (or click Actions above once you’ve selected your current security group) and select “Edit inbound rules”.
There you can select HTTP and set the rule to allow traffic from Everywhere. Else no one will be able to access the sites you’re hosting.
That’s pretty much all I had to change before I got this blog site up and running! I’ll continue posting more, especially since I’m ramping back up my Node.JS love!