Records Lab
Goal
Learn how to connect a Flask Python app with a Postgres database using the records module.
Setup
- If you haven’t already, you should accept the assignment through Moodle for the ID3: individual back-end deliverable since you’ll be working there today.
You should edit most of these files locally and push them to your individual repository before then connecting to stearns (through fern) and testing them out.
Remember that if you’re on stearns and want to push, you’ll probably need to use a GitHub Personal Access Token to do so.
It’ll generally be easier to just create files locally and not need to push from stearns.
psql_config.py
The module we’ll be using will allow for connection between your database and your Flask app. However, for it to make that connection, it needs to know the relevant information about your database, including your database password. In general, you should never push passwords to a git repository, so we’re going to first verify that you can’t accidentally push this new file with that information to the remote repository.
- Open the file
.gitignorethat should be in your repository - Verify that the line
psql_config.pyis in there (this tells git to not add this file even when you dogit add *, which you should generally avoid). If you’ve been having trouble with__pycache__files getting into your team repository, you can always add that to your.gitignore! (If you do, be sure to add/commit/push)
datasource.py
I’ve provided a file named datasource.py in your repository.
This file is responsible for connecting your database to your Flask app.
- In that file, there are two import statements, the first imports the module we’ll be using and second is the configuration file that you will make on
stearns:import records import ProductionCode.psql_config as config -
This file is structured in an object-oriented style, so there is a class called
DataSource:class DataSource: def __init__(self): pass - The responsibility of this class is to connect to your database and
recordsmakes that pretty easy with the following code (already provided):connect = f"postgresql://{config.user}:{config.password}@localhost:5432/{config.database}" self.db = records.Database(connect) -
Your constructor creates a connection string using your log in information from
psql_configand sets up the connection to listen locally at port 5432 (the default PostgreSQL port). It then usesrecordsto connect to the database and saves the resultingDatabaseobject as an instance variableself.db. - You’ll need to write a method to execute one of your queries on your database and print the resulting information. To execute a query on the database, you send your exact query to the
querymethod:def get_example(self): result = self.db.query("SELECT * FROM test WHERE second_col ='data_2_2' ") print(result[0])Edit this example to send a query to either your ID3 data table or the
pokemondata table from Monday. Then add, commit, and push your file.
On stearns
Now you’re ready to test out your code with your database.
-
Connect to stearns (through
fern) and clone your individual repository. Make sure that your.gitignorefile is there so that you don’t accidentally push your database information. -
Create the
psql_config.pyfile - Add the following three lines with your individual database information:
DATABASE = 'YOUR_DATABASE_NAME' USER = 'YOUR_CARLETON_USERNAME' PASSWORD = 'YOUR_DATABASE_PASSWORD'That should be your Carleton username for the first two and the password that I provided to you for the last one.
- Test out your method by calling it in
datasource.py’s main to see what you get. If you need to make changes, remember to make them locally and the push to test.
app.py
- Remember to go back to editing locally at this point. You likely noticed that the result is of this type
Recordwhich isn’t quite what your Flask app is used to getting. You can export records in lots of formats, but let’s test outcsvsince you are used to that already. Update your code to useexport('csv')like so:print(result[0].export('csv')) -
You ultimately want to be able to return data from your
Datasourcemethods to your Flask app, so switch to returning and then openapp.pyand test out calling your method. (Remember to add, commit, and push and the pull down your changes onstearnsto test.) -
Try running your
app.pyand see if it is able to connect to your database and print the results to the terminal! - If that works, great job, you’re almost done with your individual deliverable! (You might need to do some additional style fixes still.)
Once you finish with the individual deliverable, start thinking about the changes to your team project that will be needed to connect your Flask app to your new database.