Querying the Database
More information about `mbpy extract from-db`
To query the database, mbpy needs the data imported first, which can be accomplished with the Importing commands.
Extracting
The extract
command has a variety of means to load data, either from the API by querying it directly, or by querying the SQlite database. We can even read in CSV files.
mbpy extract --help
Usage: mbpy extract [OPTIONS] COMMAND [ARGS]...
Start ELT sequence. See mbpy extract --help for extaction commands
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ from-api Pass a name like students, classes, academic_info_grades โ
โ from-csv Define the path to csv file โ
โ from-db Select entities (and associated entities), fields โ
โ from-db-connect use raw sql โ
โ from-excel Define the path to csv file โ โ
โ from-external-api Call out to a list endpoint. Default values work for OA V3 โ
โ students list endpoint. For OA, use --bearer and pass in the โ
โ bearer via environment variable token after /oauth/token call. โ
โ FIXME: Add this here โ
โ from-pipeline Use pipelines as an extraction tool for more sophisticated โ
โ data management. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
We'll use the from-db
command to demonstrate how it works, but as you can see there are some other powerful features.
There is extensive help documentation included with mbpy, available here:
Let's find out how to use this command:
mbpy extract from-db --help
โญโ Additional Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ * --table -t (academic_terms|academic_years|classes|class_attendance_byterm|c โ
โ lass_attendance_bydate|class_timetable|hr_attendance_byterm|hr_a โ
โ ttendance_bydate|memberships|modified_since_dates|parents|parent โ
โ child_relationships|school|school_subjects|students|subject_grou โ
โ p|tasks|task_results|teachers|term_grades|year_groups) โ
โ [required] โ
โ --fields -f Fields of the entity to include โ
โ (TEXT) โ
โ --limit -l Define small number for faster dev โ
โ (INTEGER) โ
โ --help Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
We just have to pass it a --table
option that includes the name of the entity we are seeking, in this case classes. Since we are starting to build complicated commands, let's use the \
so we can format using line breaks and spaces, making it easier to read:
mbpy \
EXTRACT from-db \
--table classes \
INFO
Column info for dataframe
โท โท โท
โ type โ count non-null โ row 0
โถโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโด
id โ int64 โ 366 โ 11933215
subject_id โ float64 โ 365 โ 4509.0
end_term_id โ int64 โ 366 โ 180452
start_term_id โ int64 โ 366 โ 180451
program_code โ object โ 366 โ diploma
grade_number โ int64 โ 366 โ 13
subject_group_id โ float64 โ 365 โ 1709.0
...
subject โ object โ 335 โ {'name': 'English', 'sl':
โ โ โ True, 'self_taught': False,
โ โ โ 'group_id': 1709, 'group':
โ โ โ 'Language Acquisition', 'hl':
โ โ โ True, 'program_code':
โ โ โ 'diploma', 'levels': ['ab
โ โ โ initio', 'B'], 'id': 4509}
reporting_name โ object โ 366 โ Happy English Class
Chaining
The above output is the default when plain target INFO
is used. It selected all the classes that have been imported and converted it into a pandas dataframe. Since we haven't told it to do anything other than that, the default action of displaying info about the dataframe, such as the column names, types, and count of non-null, is shown. The values for the first selected item is shown in "row 0" column.
There's simply too much information, so let's limiting the columns we are displaying, and filtering for just diploma classes:
mbpy \
EXTRACT from-db \
--table classes \
--field-mode select \
--fields uniq_id,program_code,grade_number,subject \
--limit 3 \
PRINT \
query \
--where 'program_code == "diploma"' \
โท โท โท โท
โ uniq_id โ subject โ grade_number โ program_code
โถโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโด
0 โ happy_english_class โ {'hl': True, 'name': โ 13 โ diploma
โ โ 'English', 'group_id': โ โ
โ โ 1709, 'levels': ['ab โ โ
โ โ initio', 'B'], โ โ
โ โ 'program_code': โ โ
โ โ 'diploma', 'sl': True, โ โ
โ โ 'id': 4509, โ โ
โ โ 'self_taught': False, โ โ
โ โ 'group': 'Language โ โ
โ โ Acquisition'} โ โ
โต โต โต โต
Okay, what about querying to find all the high-level classes? The subject column has the key hl
, but how do we access to query it?
This is where the power of tranformations comes in. Just like the query
command after the chain
command, mbpy has a slew of operations that can be performed on the query. There is a spread
command that will make a new column for every key, and each column will be called subject.hl
and subject.levels
etc.
mbpy \
EXTRACT from-db \
--table classes \
--fields uniq_id,program_code,grade_number,subject \
--limit 3 \
INFO \
spread
Column info for dataframe
โท โท โท
โ type โ count non-null โ row 0
โถโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโด
subject.id โ int64 โ 3 โ 4509
subject.self_taught โ object โ 1 โ False
subject.hl โ bool โ 3 โ True
subject.group_id โ int64 โ 3 โ 1709
subject.group โ object โ 3 โ Language Acquisition
subject.levels โ object โ 1 โ ['ab initio', 'B']
subject.program_code โ object โ 3 โ diploma
subject.sl โ bool โ 3 โ True
subject.name โ object โ 3 โ English
uniq_id โ object โ 3 โ happy_english_class
program_code โ object โ 3 โ diploma
grade_number โ int64 โ 3 โ 13
So now we can filter for hl classes, and let's arrange the columns in the order we wish to be output, and limit them to just the ones we're interested in:
mbpy \ git:main*
EXTRACT from-db \
--table classes \
--fields uniq_id,program_code,grade_number,subject \
--limit 3 \
PRINT \
spread \
query --where '`subject.hl` == True' \
keep -c uniq_id -c subject.hl -c program_code -c grade_number \
to-csv --path /tmp/output.csv
โท โท โท โท
โ uniq_id โ subject.hl โ program_code โ grade_number
โถโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโด
0 โ happy_english_class โ True โ diploma โ 13
โต โต โต โต
Not only did we get this output in the terminal, but it saved to CSV as well.
To learn more about what kinds of commands are available, use the --help
feature. The following shows the help files for every command issued on this page:
mbpy extract from-db --help
mbpy extract - chain --help
mbpy extract - chain query --help
mbpy extract - chain print --help
mbpy extract - chain spread --help
mbpy extract - chain keep --help
mbpy extract - to-csv keep --help
The next section will demonstrate how to save this query for easy execution.
Querying via ScriptIf you are interested in learning more about the extract โฆย chain โฆย formula, skip to this section:
Querying CommandsLast updated
Was this helpful?