Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Introducing the Pyramid framework
Lecture: Building block: Configuration
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Configuration's a super important part of our web app.
0:03
Like I described in the intro to this chapter
0:05
there's certainly behaviors you would like
0:08
to be different in development than in production.
0:12
A real simple is, do you send email notifications to users
0:17
when you take certain actions?
0:18
Well, in production, of course you do that, right?
0:21
I want to reset my password, or I purchased this thing.
0:24
If you're doing testing locally, especially if somebody
0:27
comes to you and says, hey, the site's not working for me,
0:31
I tried this, but this happens, you might want
0:33
to log in as that user and then try that action.
0:37
And obviously you don't want them to get the email.
0:41
The easiest way to do this in Pyramid is to have
0:44
different configuration files, both for development,
0:46
production, test, whatever the various scenarios you have are.
0:50
In fact, when you create a new project,
0:52
it comes with a development.ini and a production.ini,
0:55
and in here we have a bunch of settings
0:58
that Pyramid uses to control itself.
1:00
But we can add other settings just by saying
1:03
key equals value, so db_file=/db/webapp.sqlite,
1:10
API key equals whatever that thing is.
1:12
Notice there's no quotes on the strings,
1:14
they just come back as strings anyway.
1:16
Super easy, we can do that.
1:18
Now how do you get to them in Pyramid?
1:19
Well, we saw that main startup method,
1:22
and the settings being passed in there,
1:24
so that's super easy.
1:25
We go to that again.
1:26
Now we can go to that config and say get us the settings,
1:29
this is a dictionary that has those keys and values.
1:33
So if I want the DB file, I just say configsettings.get,
1:37
and there's just standard Python dictionary access.
1:40
Get me the thing for DB file, get me the API key,
1:44
and off you go.
1:45
And obviously these can be different
1:47
in development and production.