[TOC]
Fork the repository from BitBucket and rename to your project.
If working on a local machine, then clone the repo from your terminal
git clone https://username@bitbucket.org/username/repositoryname.git
source setup.sh
python create_db.py
python app.py
NOTE: The application is running over HTTPS. Therefore, make sure to change the link from bash http://IP_ADDRESS:8080 to https://IP_ADDRESS:8080
NOTE: You may need to change the username and password located on lines 19 and 20 of app/secret_config.yaml
to match your SQL login information.
You can now check your localhost to see if it deployed correctly.
source setup.sh
python create_db.py
python app.py
If you are succesful you will see something like:
Starting application
Running server at http://0.0.0.0:8080/
Click the link in your terminal to check if it is deployed correctly.
- Project Name
- App
-static
-templates
-start.html
- __init__.py
- allImports.py
- config.yaml
- models.py
- start.py # this an example of a python file that renders a page
- Data
- db.sqlite
- Venv
- app.py
- create_db.py
- setup.sh
Above you will find the file structure for the flask template. You will be mostly working with the app/ directory. Some important files and directories.
models.py - This file contains the database schema or the tables and columns that will be in database. If you want to make a new table then you will add a class to this file, see the example in the models.py file. Once you are done making changes to this file run create_db.py to make the changes in the database.
App/ directory - This directory will contain a python module in order for python files to be recognized they must be added to the __init__.py file in this directory.
start.py - This file is a very quick example of a python file that will render a page. This file processes and renders the start.html file located under templates.
If I wanted to create a new webpage then I would do the following.
from allImports import *
@app.route("/example", methods = ["GET"])
def example():
return render_template("example.html", cfg = cfg)
{% extends "base.html" %}
{% block body %}
Example Paragraph
{% endblock %}
* Import the python file you created inside the \_\_init\_\_.py file.
```Python
from app import example.py
In order to read from a database you will need to make a query to get the data. You can find out more about queries at the peewee site one quick example of a query would be the following:
query = tableName.get( condition = something )
This will return a python object that will have the data as attributes. You can pass this object to the html file. You can access this data by typing query.Column.
NOTE: Needs more details on asking query to finish reading and writing to the database.
If it is directed towards sqlite, the code to create the database connection will look like this:
from app.loadConfig import *
here = os.path.dirname(__file__)
cfg = load_config(os.path.join(here, 'config.yaml'))
db = os.path.join(here,'../',cfg['databases']['dev'])
# mainDB = SqliteDatabase(cfg['databases']['dev'])
mainDB = SqliteDatabase(db,
pragmas = ( ('busy_timeout', 100),
('journal_mode', 'WAL')
),
threadlocals = True
)
Once you run the migrate_data script, if successful, all data from the db.sqlite should have migrated to the mysql database created at step 1.
Now, to run the application with the newly created mysql table, make sure that models.py is directed towards mysql: If it is directed towards mysql, the code to create the database connection will look like this:
from app.loadConfig import * dir_name = os.path.dirname(file) # Return the directory name of pathname file cfg = load_config(os.path.join(dir_name, 'config.yaml')) db_name = cfg['db']['db_name'] host = cfg['db']['host'] username = cfg['db']['username'] password = cfg['db']['password']
mainDB = MySQLDatabase ( db_name, host = host, user = username, passwd = password)
**** I would highly suggest that you keep two models.py files one with sqlite and one with mysql so you could transition between the two if needs be.
If when you run the application, you don't have access to the admin menu, simply edit Scott Heggen's isadmin field to 1 using the steps below.
With mysql, you will not be able to use DB browser to visualize the data like we used to with sqlite. You will have to run: