Creating a URL Shortener with Flask - Part 1
03 Dec 2015I wanted to play around with python more in terms of web application, rather than a scripting language.So i decided to create a URL shortener to understand the frameworks better. I chose flask because its a simpler framework than Django and is well supported.
Here are the steps i followed
Setting up the enviornment.
-
Create a folder for my project
mkdir UrlShortener
-
I wanted to use a virtual env as it helps managing dependencies better.
Pradheep github $ cd UrlShortener/ Pradheep UrlShortener $ virtualenv venv New python executable in venv/bin/python2.7 Also creating executable in venv/bin/python Installing setuptools, pip, wheel...done.
Now we have all the virtual enviornment all set and ready to go.
-
Finally i activate the virtual enviornment by using activate command as follows
Pradheep UrlShortener $ source venv/bin/activate (venv)Pradheep UrlShortener $
-
This project is going to use pymongo, flask so i did the following
pip install Flask pymongo coverage
-
We then initialize an empty git repositary using git init
git init .
With this we have setup all the necessary tools.
-
I created a github repo and i pulled the repo to my local directory.
git remote add origin https://github.com/PradheepShrinivasan/UrlShortener.git
git pull
With this the entire setup basic setup of our system is done.
Setting up Flask
-
Create folders for application
mkdir app app/static app/template tmp
-
create an
app/__init__.py
file with the following contentsfrom flask import Flask app = Flask(__name__) from app import views
-
create a view file in
app/view.py
from app import app @app.route('/') @app.route('/index') def index(): return "URL Shortener"
-
Now to start the application, we create
run.py
which starts our application#!flask/bin/python from app import app app.run(debug=True)
-
The directory structure of application is as follows
$tree ./ -l 2 ./ ├── LICENSE ├── app │ ├── __init__.py │ ├── static │ ├── template │ ├── views.py │ └── views.pyc ├── run.py ├── tmp └── venv
-
Now run the application using the following command
$ python run.py * Restarting with stat * Debugger is active! * Debugger pin code: 163-315-710
-
Go to a web browser and look up
http://localhost:5000
and you will be able to will see “ URL Shortener!” -
Commit all changes to you have made till now to git master branch
Deploying to heroku
-
Install heroku toolbelt if you have not already installed.
-
Create add a remote of
$ heroku create Creating enigmatic-temple-7361... done, stack is cedar-14 https://enigmatic-temple-7361.herokuapp.com/ | https://git.heroku.com/enigmatic-temple-7361.git Git remote heroku added
-
Create a requirement.txt that helps installtion on heroku by following command
$ pip freeze > requirements.txt
-
Create a Procfile that helps heroku identify how to start the application
web: python ./run.py
-
Now change the
run.py
a bit to make it run on heroku. Heroku expects that you use thePORT
enviornment variableapp.run(host='0.0.0.0', port=int(os.environ['PORT']), debug=True)
-
commit all changes and push the changes to heroku using
git add Procfile requirments.txt run.py git commit -m "add changes to Procfile, requiements.txt" git push heroku master
This moves the code to heroku and start the application
-
Now open the application using
heroku open
heroku open
You must see the same output as you have seen earlier.
With this we have completed installation of application on Flask and hosted it on heroku.
The heroku hosted site is here
Reference:
I looked up this micro blog for some ideas and basic structure of the flask application. I wish i would write a full blown application like the author and i highly recommend reading the blog