Posted on 2020-06-13T11:37:25Z by Kevin Foong
In this post we will explore some simple SQLAlchemy statements and compare them to their SQL equivalents. This post is just on using Flask SQLAlchemy to query your database. If you want to know how to model your database please see my other post here. Let's get started!
Let's say we have two tables UserDetails
and Country
in a one to many relationship towards Country
. That is one UserDetails
can only have one Country
. But one Country
can have many UserDetails
. Our models look like this.
UserDetails
class UserDetails(db.Model):
__tablename__ = 'user_details'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
first_name = db.Column(db.String(50), nullable=False)
last_name = db.Column(db.String(50), nullable=False)
gender_id = db.Column(db.Integer, db.ForeignKey('gender.id'), nullable=False)
dob = db.Column(db.String(10), nullable=False)
country_id = db.Column(db.Integer, db.ForeignKey('country.id'), nullable=False)
state = db.Column(db.String(30), nullable=False)
city = db.Column(db.String(30), nullable=False)
def __repr__(self):
return '<UserDetails {}>'.format(self.first_name)
Read more ...
Tags:
flask
flask-sqlalchemy
database
Posted on 2020-06-02T06:30:03Z by Kevin Foong
This is the second part of my tutorial to create a fully functioning user registration and login form in Flask. In the first part of the tutorial we looked at the backend side especially Flask SQLAlchemy and the database. I assume you have already set up everything from the first part and this will just be a continuation of it. In this part we will focus on the Flask side, in particular Flask WTF and Flask Login. Part 1 is available here.
1. First install flask-wtf and flask-login in your project's virtual environment. "wtforms[email]" is just an extra package from wtforms which provides email validation which we will use later.
pip install flask-wtf
pip install wtforms[email]
pip install flask-login
Read more ...
Tags:
flask
mysql
flask-sqlalchemy
flask-login
flask-wtf
Posted on 2020-06-01T12:33:45Z by Kevin Foong
In this post we will go through how to create a fully functional login and registration form in Flask. Part 1 (this post) will go through the backend part - setting up Flask, Flask-SQLAlchemy and creating the database. In part 2 we will build the actual forms and templates and see this in action.
In this tutorial we will start from the beginning, assuming that you only have Python installed. This tutorial was created using Windows but should be similar for other operating systems.
Initial setup
1. Create a folder for your project. Then in this folder create a Python virtual environment.
python -m venv env
2. Activate your virtual environment.
env\scripts\activate
3. Install the following packages.
Read more ...
Tags:
flask
flask-migrate
mysql
flask-sqlalchemy
Posted on 2020-05-17T00:45:41Z by Kevin Foong
This is part 2 of the Git and Github intro. See part 1 here.
If you are contributing to more than one Github account, for example one for work and one for home, you may need to set up SSH keys, one for each Github account to prevent permission denied error's and so that you don't have to keep re-entering your username and password. The below are instructions for Windows users on how you can do this.
- If you downloaded Git for Windows you will probably have “git bash” installed. Start git bash.
- Generate a ssh key
ssh-keygen -t rsa -C youremail@email.com
- It will ask “Enter file in which to save the key”. Save it to the below location. Ensure that you give the key some unique name (swap your values with the ones in UPPERCASE).
C:\Users\USERNAME\.ssh\id_rsa_UNIQUENAME
Read more ...
Tags:
git/github
Posted on 2020-05-16T09:26:10Z by Kevin Foong
Here is a quick introduction to Git and Github for beginners. Say you have been developing locally and now want to upload your files to Github, you can follow these steps to create a repository and upload your files.
First create a new local Git repository for your project.
git init
Next show all the files that have been amended since the last commit. If this is your first commit, it will show all files.
git status
At this stage make sure that you have a .gitignore file in your project folder to ensure certain files and folders are ignored by Git and not sent to Github. Some examples may include the below. Once you have a .gitignore file, do git status again and you should see those files/folders omitted.
Read more ...
Tags:
git/github
Posted on 2020-05-14T06:14:20Z by Kevin Foong
Here is how to do a game or animation loop in Vue using Javascript's "requestAnimationFrame" method.
So what is requestAnimationFrame?
requestAnimationFrame
is a method that is part of the very large Web API's under the Window Interface. See this page for more information - https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame
This method provides a callback, typically to the function it is in, to create a recursive loop that can achieve up to 60 frames per second. I've created a very simple growing box animation in Vue to demonstrate.

Read more ...
Tags:
games
Vue
Javascript