4: Install Flaskinni

Expectations

The goal is easy to understand at least: Let's get Flaskinni running. The expectation here is for you to sweat the install. Setting up your coding environment is a big ol' barrier to getting started as a programmer. But like any academic pursuit: Stay cool and keep at it. My old teacher used to say: If you don't know what to write next, you haven't read enough.

Learning Target

  • I can run Flaskinni after having cloned its code, configure its dependencies, set up a database, and ran the app from a virtual environment.

Install Party

A day before some hackathons or events like RailsBridge, experts will host a session to help everyone install the tools they'll need. If you serve pizza, you can call pretty much anything a party.

Your OS

Whether you're using Windows, macOS, or Linux, you have a responsibility to your teammates that you keep your machine updated and organized. Run system updates, keep files off your desktop, have a system of organizing your project files. Knock this out early so it doesn't jam you up while installing complex programming tools.

Dev Tools

Go slowly. Go patiently. Things may not work. You will have to Google stuff, study, maybe ask for help, and solve your problems. Keep hacking at the problem. That's how we all learn best.

Python

The only way your computer understands Python code is through a compiler or interpreter. Your computer uses bytecode to run software. We have an interpreter that changes our beautiful Python code into a jumble of machine instructions.

The latest version of Python could break Flaskinni

As of June 2020, the latest version of Python that works 3.7. After that, one of the dependencies breaks down, (I've reported the issue). You'll have to download 3.7 and not the most recent one that the website suggests.

Postgres

This is the open-source database tool we're going to use. You'll set up Postgres to host your own private database. It won't be available outside of your computer. It's just so while you work on an app, it can access a practice source of information.

Remember postgres password. Since my computer's database is only used for building and testing apps, I set the password also to postgres

Git / GitHub

Mac users should have Xcode and run xcode-select --install in their terminals. Windows users need to install Git for Windows. This gives you the commands to clone a repository, manage your changes to code, and sync

NPM

Once upon a time, Javascript was just used to make pictures slowly spin on a webpage. Now we've got NodeJS letting this language that used to be purely stuck inside a web browser to now run independently on your computer. It's so popular, the npm tool to quickly install awesome new Node packages is a must-have.

Choco & Brew

Node Package Manager often encourages the installation of amazing CLI installers like choco or brew (Mac and Windows respectively). So if you like quickly installing awesome things and can accept the responsibility to do so safely, get one!

https://chocolatey.org/ (Windows) https://brew.sh/ (Mac)

Scss

Old-school CSS is a pain to program at large scale. Sass is much more powerful. Your computer has to have it to run the WebAssembly service built into Flaskinni.

Editor

VS Code

The IDE de jour is Visual Studio Code. I'm a big fan.

I recommend these extensions

Setup Code

You can edit your code on the cloud with cool sites like SourceLair. A cloud-based IDE is likely going to be based on Linux. That means you'll need to know a bit about Linux CLI. Linux plays well with coders and a cloud-based IDE makes it easy to share your development progress with people and get feedback faster. That's big.

You can also set up right on your computer. You should know how to do this just so you can practice putting all the pieces together. I prefer to program in the cloud and locally and use GitHub to keep the two environments in sync.

Clone the repo

Use VS Code's git tool to pull down the repo from GitHub.com: https://github.com/dadiletta/flaskinni

Stand up the db

"Stand up" a database just sounds so much cooler than "set up," but it's the same idea. You can see in settings.py that Flaskinni by default is going to reach out to a PSQL database named db-flaskinni on the server 0.0.0.0. The username and password used to access the database are also loaded there. Changes to this information shouldn't be done in settings.py but rather a .env fille. But before we do any of that, fire up pgAdmin and create a database.

Env variables

.env
.env
###################
## FLASK
###################
FLASK_ENV=development
FLASK_APP=flaskinni
###################
## DATABASE
###################
DB_HOST=0.0.0.0 # for windows use 127.0.0.1
SECRET_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
DEBUG=True
DB_USERNAME='postgres'
DB_PASSWORD='postgres'
DATABASE_NAME='db-flaskinni'
###################
## FLASKINNI
###################
UPLOADED_IMAGES_DEST = '/Users/mra/Projects/flaskinni/app/static/images'
# windows looks more like this:
# UPLOADED_IMAGES_DEST = 'c:\\Users\\mra\\Projects\\flaskinni\\app\\static\\images'
UPLOADED_IMAGES_URL = '/static/images/'
# STARTING ACCOUNTS
STARTING_ADMIN_PASS = 'flaskinni123'
ADMIN_EMAIL='flaskinni@gmail.com'
###################
## FLASK-MAIL
###################
MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=465
MAIL_USE_SSL=True
MAIL_USE_TLS=False
MAIL_USERNAME='flaskinni@gmail.com'
MAIL_PASSWORD='xxxxxxxxxxx'
MAIL_DEFAULT_SENDER='flaskinni@gmail.com'
###################
## FLASK-SECURITY
###################
SECURITY_PASSWORD_HASH ='pbkdf2_sha512'
SECURITY_PASSWORD_SALT = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

Virtual env

First you use the correct version of Python to launch its venv module to create a folder also named venv: python3 -m venv venv

/c/Users/danad/AppData/Local/Programs/Python/Python37-32/python -m venv venv

Then we activate the virtual environment so all our required "expansion packs" or dependencies will be installed in our little bubble:

source venv/bin/activate (mac) source venv/Scripts/activate (Windows running bash) venv\Scripts\activate.bat (Windows running PowerShell)

Lastly, we run pip install -r requirements.txt

Did one of the dependencies listed in requirements.txt fail to install correctly? It's a common problem that must be solved. uWSGI doesn't need to be installed in the development environment so you can #comment that line out. If psycopg2 is failing to install, you can comment that out and manually install the binary alternative, pip install psycopg2-binary. Running pip install wheel may also resolve many of the installation issues.

Flask run

After running flask run in VS Code, you should be able to visit http://127.0.0.1:5000 to see your website.

Migrations

This is important! Pity the poor programmers that forget to set up their migration tools. Once Flaskinni is up and running the first time, run flask db init to allow Flask-Migrate to help with any future changes to your models. We'll talk about why that's super important later.

Alternate setups

Docker

I need to study up on Docker. These are notes that I'll use to redo this section later. It's important to try new stuff, even if you're not moving it into your stack. Docker represents a booming growth in containers, and that's something serious web dev folks need to deploy at scale.

  1. Make sure Docker is installed, running and logged in.

  2. Clone Flaskinni git clone http://github.com/dadiletta/flaskinni

  3. Create a private.py file inside the flask and a docker.env file

    docker.env ( POSTGRES_USER=ubuntu POSTGRES_PASSWORD=ubuntu POSTGRES_DB=db DB_HOST=db

  4. Optional: docker-compose up --build to test to see if Flaskinni will run before you change its name.

  5. Change name from flaskinni to your project’s title (lowercase)

    1. Change the name of the folder

    2. uwsgi.ini should be the same as the name of the folder

    3. docker-compose.yml should be changed to match the name of the new folder in the volume section. The container name should also be adjusted.

    4. Replace all python references to flaskinni to your new folder name

      1. manage.py (we no longer use manage.py)

      2. inni/views.py

      3. Dockerfile

  6. Security

    1. Change the name of the db in docker.env and match it in private.py

  7. docker-compose up --build

SourceLair

At Gilmour, going to set you up with a pre-configured environment. You're pretty much all set. You'll need to create a private.py file with the database login details. You'll also need to create a file named .env with at least one line, DB_HOST=postgres

What if it's not working?

You're going to get stuck... a lot. This is where you can distinguish yourself as a programmer. Every problem big and small you hack your way through makes you more legit. So enjoy the grind. It's important to read around the problem and not just through it. Sloppy copying from StackOverflow once you copy and paste the error into Google can get you in trouble. Get background research done to understand some of the bigger concepts. Be willing to reach out for help, but respect that other people are grinding away too. Always show evidence of your research when asking for help as it will build trust with your support network.