Skip to main content

Code base to jump start flask-classy system

This post is just to announce my sharing of a source code base which can be used to jump start development using the python micro framework flask and it's excellent extension flask-classy. Do note that this code base uses the Ace Admin bootstrap theme. Please purchase the license for your use from wrapbootstrap or remove it from your source code referencing it.

To begin, you need to clone the repository using mercurial:
hg clone https://bitbucket.org/abdza/flask-classy-base-system examplesystem

Then you need to create the virtualenv for your system:
virtualenv --python=/usr/bin/python2 examplesystem

Do note that my example here is running on arch linux which uses python3 as the default python version. I suggest the above command to use python2 in your virtualenv because it makes things easier to migrate to systems using python2 (eg ubuntu, centos and others).

The you need to go into the directory and activate the virtualenv:
cd examplesystem
source bin/activate

Then you need to install all the required python modules into your virtualenv:
pip install -r requirements.txt

Once all that is done you need to configure your database access by editing the file app/config.py
Update the SQLALCHEMY_DATABASE_URI line with your own database setting:
SQLALCHEMY_DATABASE_URI = 'postgresql://<username>:<password>@localhost/<database>'

So for this example I changed it to:
SQLALCHEMY_DATABASE_URI = 'postgresql://exampleuser:examplepass@localhost/exampledb

You'd probably want to update the SECRET_KEY, APP_NAME and COMPANY_NAME too.

Then you need to create the appropriate postgres database, role, grant and allow the user to login.
sudo -u postgres psql
postgres=# create database exampledb;
postgres=# create role exampleuser with password 'examplepass';
postgres=# grant all on database exampledb to exampleuser;
postgres=# alter user exampleuser login;
Then you can test the user and database connection:
postgres=# \c - exampleuser
postgres=> \c exampledb
If there is no error, then you can continue to the next step after you quit postgres:
exampledb=> \q
You then need to init the database by running:
./run.py db init
./run.py db migrate
./run.py db upgrade
That should get your database updated with all your models (which currently is only User)
You can try running it now:
./run.py
Then in your web browser go to the address below and register your new user:
http://localhost:5000/user/register

After all of that, you should probably edit your .hg/hgrc file and point the default path to your own repository.

Once everything is set, you can begin your development. Add a new model by adding a file in the app/models directory. For example lets add the file app/models/Post.py:
from app import db

class Post(db.Model):
 id = db.Column(db.Integer,primary_key=True)
 name = db.Column(db.String)
 content = db.Column(db.Text)

Then you need to tell your app about the existence of this model by adding the following line to app/models/__init__.py:
from Post import Post
Once you have added your model files, you can generate the view associated with that model:
./run.py genview

You can then see the generated view files at app/views/PostView.py

Before you can view the view in your web browser, make sure your database get updated first with your newly added model:
./run.py db migrate
./run.py db upgrade
Then you can view the view page by pointing your web browser to:
http://localhost:5000/post

You can then start editing the view for your model and your processes. For example we can add the Post field in your form by changing the ItemForm class in your app/views/PostView.py:
class ItemForm(Form):
 name = StringField('Name',validators=[DataRequired()])
 post = TextAreaField('Post')
That's all for the introduction to this code base. Hope it is useful for you to jump start your development using flask and flask-classy.

Comments

Popular posts from this blog

Food first post

My blogs' name is High-Tech Rojak but I don't recall ever talking about food. So here's a first. Just recently I got some free time and finally got to cook the pasta I've bought for ages. So here's an account of how it went down.. :) Okay. That's the thing I cooked. I have no idea what it's called. At first I was thinking of buying the ribbon ones, then my wife looked at the colorful spiral ones and said "why not get these? they're more colorful" and so we got them (yes, we know nothing about pasta.. :) So I boiled it, actually put some salt and oil into the water so that they won't stick, drained it and tadaaaaa, you'd get the above. I remember once I tried to cook macaroni and I didn't drain it after boiling it, it filled up the whole pot. LOL... Learned my lesson. Next up the sauce. Like I said we don't know anything about pasta so here's the ingredients we prepared. Yes ladies and gentleman. Instant pasta sauce all bottl

Documentation is a must... after this.

I've been thinking quite a bit about documentation and the 'cost' it involves. And when I say documentation, I mean documentation in general about anything. One obvious case with the industry I'm involved in is user documentation (a.k.a The Manual). Creating great features in software takes time and effort but if it is not documented then the user won't even know about it and finally it never gets used. But then while documenting it you just wish that you're working on the next cool thing rather than have to write this up. So finally you end up not doing the documentation or doing it rather badly. Same thing with this blog writing. I have been doing some pretty interesting things with my phone (rooting it and using cynogenmod and all), some pretty significant life changes (my grandmother passed away) and a lot of other things which I should probably like to remember better or reflect more on it but not documented (here or anywhere permanent) and it would probabl

The Future Of Gaming

I love playing computer games. It's what originally drove me to learn computer programming, I wanted to create my own games. Until now I still have very little success with that, but... I have learnt to program web applications quite well and earning my pay using those skills. And I love open source software. Ever since I started programming professionally, my main work OS has always been Linux (various distributions and all and currently on Arch Linux). I always install dual-boot because... hardware problems (some projectors and printers just couldn't be detected by Linux when I started out, that's mostly not a problem now) and mainly to play games (sure there was some open source games available, but apart from "Battle for Wesnoth" and "FreeCiv" I don't actually recall any games I've played extensively enough to be remembered). But recently the gaming scene in LinuxLand has improved tremendously, partly thanks to the Windows 8 app store like