- 23 Aug 2023
- 7 Minutes to read
- Print
- DarkLight
Reports with Parameters
- Updated on 23 Aug 2023
- 7 Minutes to read
- Print
- DarkLight
Parameterized reports are a tool to help the user more easily use compound reports. By entering parameters within the report code, it becomes possible to use the same compound report for multiple similar reports. For example, it is possible to write a report by entering all the date parameters at one time instead of writing nearly the same report multiple times or altering the code of the same report multiple times to change a date range.
Creating Reports with Parameters
To use parameterized reports, it is important to follow some strict guidelines:
- Begin the section with
params.add()
. - List the name of the parameter. The first argument in each parameter must always be the name. Naming conventions require the name to be within quotes, begin with a lowercase letter, and separate additional words with either an underscore or an uppercase letter.
params.add('my_name')
OR
params.add('myName')
- Add the type of data for the parameter. Each parameter can only have one type, but each must have a data type.
Use quotation marks around each type as it is entered into the code:
params.add('myName', type='str')
- Add the optionsfor that parameter within the parentheses. Each item in the parameter must be separated by a comma.
Use quotation marks around the information provided for each option with the exception of True/False, integers and floats:
params.add('facility_name', type = 'list', choices=['North', 'South', 'Mountain', 'Desert'], default='North')
- End the parameters section of the report's code with
params.end()
. - Assign the value of the parameter to a variable that can be used elsewhere in the compound report if desired. For example:
mode = params.parameters['parameter_name'].value
. Mode can be any name desired, and parameter_name can be any parameter desired. However,params.parameters
and.value
are constants and cannot be changed. - Finish writing the compound report script.
- Press Run below the compound report window.
If the type of parameter entered is bool it displays as a checkbox in the Parameters section of Report Builder.
When choices is used, only values specified are valid, any other will fail validation.
Types
- bool: Boolean; the value is either true or false
- date: date; date format (year, month, date), see details below
- dict: dictionary; key:value pair This currently has no visual representation and is only used for validating.
- float: number with decimal places
- int: integer
- list: list; choices can be provided, if not, enter a comma separated list after Run has been clicked
- str: string; choices can be provided, if not, provide information after Run has been clicked
Options
- choices: renders a drop-down list when an array of choices is entered, works for all types of data but Boolean and dates
- description: displays a description for the parameter, shows up as tooltip
- default: sets the default value for the field, works for all types of data
- display_name: displays the name of parameter in the parameters section
- hidden = True: hides the parameter from showing in the parameter section
- min: sets the minimum value of a field, works with integers, floats, and dates
- max: sets the maximum value of a field, works with integers, floats, and dates
- parent: controls the choices available in the child option
- child: links to the parent option, choices change depending on which parent is chosen
- optional = True: allows a null value to pass validation in a parameter, a value must still be entered in the parameter section
- precision: provides the number of decimal places a float will use, ignored by all data types but float
Formatting Date Parameters
Date parameters must use a date object for all options used. To do this, add datetime.date(yyyy, m, d)
to each option. The year must contain four characters; the month and the date may contain either one or two characters depending on need. For example, the minimum value option would read min=datetime.date(2015, 1, 1)
. A print function will automatically convert the date to a format the server understands, 2015-01-01.
If any of the options below are used in a date parameter, they must contain the date object:
- default
- min
- max
When using a date parameter, the report must enter import datetime
at the beginning of the report in the setup section of the code.
Formatting Parent-Child Parameters
Parameters can be set with parent and child options. When these options are used, two parameters are linked. The values of the second parameter change depending on the choice of the first parameter.
To create the parent-child relationship use the steps below:
- Create the lists to be used in the parent and child parameters. Though the example below does not show it, it is possible to create the lists by querying the search server.
facilities = ["Practice A", "Practice B", "Practice C", "Practice D"] providersList = ["Doctor A", "Doctor B", "Doctor C", "Doctor Q", "Doctor X", "Doctor Y", "Doctor Z"] practicesList = ["All"] + sorted(facilities) #Make a new list of the sorted facilities with "All" at the top of the list providersList = ["All"] + sorted(providersList)
- Add the parent parameter. These choices control the values displayed in the child parameter. To link to the child parameter, add
child = ["child_parameter_name"]
to the parameter. Use a comma separated list to link to more than one child.# the providers parameter is driven by the practices params.add("practices", type="list", choices=practicesList, default='Practice B', display_name="Practices", child = ['providers'])
- Add the child parameter. These values change depending on the selection of the parent parameter. Link the child parameter to the parent parameter by entering
parent = {
. Assign the correct values to each option available in the parent parameter.params.add("providers", type="list", choices=providersList, default=providersList[0], display_name="Providers", parent = { "name": "practices", "values": { "All": { "choices": ["Doctor A", "Doctor B", "Doctor C", "Doctor X", "Doctor Y", "Doctor Z"], "default": ["Doctor C"] }, "Practice A": { "choices": ["Doctor A", "Doctor B", "Doctor C"], "default": ["Doctor B"] ...#List the rest of the practices }, "default": ["Doctor Z"] } } #If no value for practices revert to normal choices and default #On selects, multiselects or unselects recalculate, union of choices, first default encountered )
- End the parameters section with
params.end()
.
It is possible to pull data from the server to provide the values for the parents and children by writing queries and calculating the lists from the queries.
Using run_with_defaults
It is possible to write a parameterized report that will automatically run the defaults without requiring the person who is executing the report to enter any additional input. By setting run_with_defaults to True the report will run with the default value and will not require the user to enter any input in the parameters unless the default values do not exist or do not pass validation. Some guidelines to using run_with_defaults are below:
- Write a report including the entire scope of information that will be required in the parameters.
- Within the
params.add
line, call the report into that parameter withchoices = report_name
. - Within the
params.add
line, set a default. - End the parameter section with
params.end(run_with_defaults=True)
.
Below the sample report is named insomnia. The choices are sorted by age, the server returns only unique values, the values have been converted from the dataframe into a list.
insomnia=get_hits('()insomnia n.age:[20 TO 60]', fields = 'extract.patient_sex, extract.age, extract.patient_id, extract.patient_full_name, extract.patient_date_of_birth')
params.add('gender', type = 'str', choices = ['male', 'female'], default = 'male')
params.add('insomnia_age', type = 'int', default = 55, min = 20, max = 100, choices = sorted(insomnia["extract.age"].unique().tolist()))
params.end(run_with_defaults=True)
print(params.insomnia_age)
In order for run_with_defaults to work, the default must pass validation and all parameters must contain defaults. If any of the defaults fail validation, the user will be provided with the parameters and asked to enter input for each parameter that failed.
Running a Report with Parameters
If the report has a parameters section, fields are displayed for additional input for those parameters before results are returned:
- Provide values for each parameter.
- Press Run with parameters.
To change the choices of the parameters after results have been viewed, make the changes in the Parameters section and click Run with parameters again.
Fixing Parameter Errors
The search appliance validates the parameters are of the correct type and within the ranges specified in the report. All fields provided are required. However, it does not return an error if a checkbox is not checked or if a string field is empty (an empty string is still a string). If the middleware encounters errors with any parameters, the field will be highlighted in red and an exclamation point will appear next to the field. Hover the mouse over the exclamation point to view the reason for the failed validation. Fix the error and resubmit.