Querying via Script
Creating convenient scripts
The prevoius section showed how to use the command line to query the database and get some output on the terminal and save it as a CSV:
Querying the DatabaseHowever, re-using the query later would be quite a hassle to have to type it again. In this section, we will use the mbpy script group of commands save our query and easily re-execute it.
mbpy scripts --help
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ list Show all saved queries โ
โ run Execute a saved query โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏBy default, it comes with a few useful scripts, which we can view:
mbpy scripts list
โญโ Scripts โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ students_id --group health-checks โ
โ students_parents --group health-checks โ
โ students_grade_year_group --group health-checks โ
โ reflections --group export-helpers โ
โ search-students-by-name โ
โ school โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏThe last one outputs this:
mbpy scripts run school
โ timezone โ subdomain โ name
โถโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโด
0 โ Asia/Kuala_Lumpur โ f1 โ FariaOneThe search-students-by-name looks interesting, let's try it:
mbpy scripts run search-students-by-name Happy
Column info for dataframe
โท โท โท
โ type โ count non-null โ row 0
โถโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโด
role โ object โ 1 โ Student
timezone โ object โ 1 โ Asia/Kuala_Lumpur
graduated_on โ object โ 0 โ None
withdrawn_on โ object โ 0 โ None
full_name โ object โ 1 โ Happy Student
student_id โ object โ 1 โ HappyStudent
state โ object โ 0 โ None
display_name โ object โ 1 โ Student, Happy
year_group_id โ float64 โ 1 โ 12014897.0
first_name โ object โ 1 โ Happy
graduating_year โ int64 โ 1 โ 2023
updated_at โ object โ 1 โ 2022-11-16T16:21:45.000+08:00These scripts we are running are actually simple text files whose contents have queries built just like in the previous section. For example, the search-students-by-name script is this:
mbpy scripts run search-students-by-name Happyexecutes this:
mbpy \
EXTRACT from-db --table students \
PRINT \
query --where 'display_name.str.contains("{0}")'The above also shows an additional instrumentation that is available for these saved queries, which is that you can use the {0} and {1} to refer to additional arguments passed to the script. If you pass don't pass any, or there is some other problem, mbpy will show you with a useful error message:
mbpy scripts run search-students-by-name
mbpy \
EXTRACT from-db --table students \
PRINT \
query --where 'display_name.str.contains("{0}")'
โญโ Index Error โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ The script is expecting arguments. Please review the code above. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏYou can also associate multiple scripts with one command, using the --group feature. For example, there is a "health check" group that performs three queries, and outputs the results. You can execute all of them this way:
mbpy scripts run health-checks
Students without student_id
โถโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโด
Empty table
0 records with 5 columns
Active students with no parents assigned
โท โท โท โท โท
โ mb_id โ grade_number โ archived โ student โ has_parent
โถโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโด
173 โ 14351728 โ 2 โ False โ Rogers, Norville โ False
1914 โ 14553487 โ 2 โ False โ Student1, Test (Wally) โ False
โ โ โ โ Walter โ
โต โต โต โต โต
2 records with 5 columns
Students with either no grade, no year group, or no hr teacher
โท โท โท โท โท โท
โ mb_id โ archived โ student โ has_hr_teacโฆ โ has_grade โ has_year_gโฆ
โถโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโโโโด
1 โ 14235240 โ True โ Student, โ False โ True โ False
โ โ โ Happier โ โ โ
958 โ 14553487 โ False โ Student1, โ False โ True โ False
โ โ โ Test (Wally) โ โ โ
โ โ โ Walter โ โ โ
โต โต โต โต โต โตIt output enough information to let me see that every student has a student_id assigned, but there are two students in Grade 1, that aren't archived, that do not have any parents associated. Also, there are two other students that don't have a homeroom teacher, nor a year group associated. Better fix that.
In order to add a script to our command, we simply navigate to the src/scripts directory in the codebase, and add the text file there. If you want to group more than one command under one group, simply create a directory.
This is the current structure:
mbpy/
โโ scripts/
โ โโ school.mbpy
โ โโ search-students-by-name.mbpy
โ โโ health-checks/
โ โ โโ students_grade_year_group.mbpy
โ โ โโ students_id.mbpy
โ โ โโ students_parents.mbpyEach script needs to end with .mbpy in order to be detected by the application.
Last updated
Was this helpful?