bmonreporter/reporter.py
def run_report_set(
        server_url: str,
        param_name: str,
        param_values: Iterable,
        working_path: Path,
        nb_template_path: Path,
        rpt_output_path: Path,
    ):
    """Creates a set of reports by cycling through a set of buildings or organizations
    and then cycling through a set of Jupyter notebook templates used to create the reports.
    Input Parameters:
        server_url: the full URL to BMON server that holds the data
        param_name: the name of building or organization parameter in the notebook template
        param_values: a list of values to cycle through for the notebook parameter.
        working_path: the Path to a working directory for use by this routine
        nb_template_path: a Path to the directory where the template report notebooks are stored.
        rpt_output_path: a Path to the directory where the final HTML reports are stored. A
            subdirectory for each parameter value will be created in this directory to hold all
            the reports for that particular building or organization.

    Returns a dictionary keyed on parameter value that lists the reports for that
    parameter value; each item in the list is a dictionary describing the report.
    """
    # extract server domain for message labeling purposes
    server_domain = urlparse(server_url).netloc

    # Make a dictionary to keep track of the reports created for each parameter
    # value.  Key is paramater value, value is list of report records.  Each report
    # record is a dictionary giving info about the report.
    report_dict = {}

    # Make file names for the calculate notebook being currently run and 
    # the HTML report that is created from that template.
    out_nb_path = working_path / 'report.ipynb'
    out_html_path = working_path / 'report.html'

    # Keep track of how many reports completed and aborted
    completed_ct = 0
    aborted_ct = 0

    # Loop through all the parameter values (e.g. buildings or organizations)
    for param_val in param_values:
        rpts = []   # a list of reports for this parameter value
        for rpt_nb_path in nb_template_path.glob('*.ipynb'):

            try:
                pm.execute_notebook(
                    str(rpt_nb_path),
                    str(out_nb_path),
                    parameters = {'server_web_address': server_url, param_name: param_val},
                    kernel_name='python3',
                )

                # get the glued scraps from the notebook
                nb = sb.read_notebook(str(out_nb_path))
                scraps = nb.scraps.data_dict

                if 'hide' in scraps and scraps['hide'] == True:
                    # report is not available, probably due to lack of data
                    continue

                # convert the notebook to html. throw an error if one occurs.
                subprocess.run(f'jupyter nbconvert {out_nb_path} --no-input --to html', shell=True, check=True)

                # move the resulting html report to the report directory
                # first create the destination file name and create the necessary
                # directories, if they don't exist.
                dest_name = Path(rpt_nb_path.name).with_suffix('.html')
                dest_path = rpt_output_path / str(param_val) / dest_name
                dest_path.parent.mkdir(parents=True, exist_ok=True)
                out_html_path.replace(dest_path)

                # Add to the dictionary of completed reports.
                rpts.append(
                    {
                        'title': scraps['title'],
                        'sort_order': scraps['sort_order'],
                        'file_name': str(dest_name),
                    }
                )
                completed_ct += 1

            except pm.PapermillExecutionError as err:
                aborted_ct += 1
                if err.ename == 'RuntimeError':
                    # This error was raised intentionally to stop notebook execution.
                    # Just log an info message.
                    logging.info(f'Report aborted for server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}: {err.evalue}')
                else:
                    logging.exception(f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}')

            except:
                aborted_ct += 1
                logging.exception(f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}')

        # sort the reports in sort order for this parameter value
        rpts = sorted(rpts, key=lambda r: (r['sort_order'], r['title']))
        # only include if reports are present.
        if rpts:
            report_dict[param_val] = rpts
        
    # log the number of completed and aborted reports
    logging.info(f'For server {server_domain}, report type {param_name}, {completed_ct} reports completed, {aborted_ct} reports aborted.')
    
    return report_dict
Creates a set of reports by cycling through a set of buildings or organizations
    and then cycling through a set of Jupyter notebook templates used to create the reports.
    Input Parameters:
        server_url: the full URL to BMON server that holds the data
        param_name: the name of building or organization parameter in the notebook template
        param_values: a list of values to cycle through for the notebook parameter.
        working_path: the Path to a working directory for use by this routine
        nb_template_path: a Path to the directory where the template report notebooks are stored.
        rpt_output_path: a Path to the directory where the final HTML reports are stored. A
            subdirectory for each parameter value will be created in this directory to hold all
            the reports for that particular building or organization.

    Returns a dictionary keyed on parameter value that lists the reports for that
    parameter value; each item in the list is a dictionary describing the report.
    
Creates a set of reports by cycling through a set of buildings or organizations
and then cycling through a set of Jupyter notebook templates used to create the reports.
Input Parameters:
server_url: the full URL to BMON server that holds the data
param_name: the name of building or organization parameter in the notebook template
param_values: a list of values to cycle through for the notebook parameter.
working_path: the Path to a working directory for use by this routine
nb_template_path: a Path to the directory where the template report notebooks are stored.
rpt_output_path: a Path to the directory where the final HTML reports are stored. A
subdirectory for each parameter value will be created in this directory to hold all
the reports for that particular building or organization.

Returns a dictionary keyed on parameter value that lists the reports for that
parameter value; each item in the list is a dictionary describing the report.
Creates a set of reports by cycling through a set of buildings or organizations and then cycling through a set of Jupyter notebook templates used to create the reports . Input Parameters : server_url : the full URL to BMON server that holds the data param_name : the name of building or organization parameter in the notebook template param_values : a list of values to cycle through for the notebook parameter . working_path : the Path to a working directory for use by this routine nb_template_path : a Path to the directory where the template report notebooks are stored . rpt_output_path : a Path to the directory where the final HTML reports are stored . A subdirectory for each parameter value will be created in this directory to hold all the reports for that particular building or organization . Returns a dictionary keyed on parameter value that lists the reports for that parameter value ; each item in the list is a dictionary describing the report .
def run_report_set(
        server_url: str,
        param_name: str,
        param_values: Iterable,
        working_path: Path,
        nb_template_path: Path,
        rpt_output_path: Path,
    ):
    server_domain = urlparse(server_url).netloc
    report_dict = {}
    out_nb_path = working_path / 'report.ipynb'
    out_html_path = working_path / 'report.html'
    completed_ct = 0
    aborted_ct = 0
    for param_val in param_values:
        rpts = []   
        for rpt_nb_path in nb_template_path.glob('*.ipynb'):
            try:
                pm.execute_notebook(
                    str(rpt_nb_path),
                    str(out_nb_path),
                    parameters = {'server_web_address': server_url, param_name: param_val},
                    kernel_name='python3',
                )
                nb = sb.read_notebook(str(out_nb_path))
                scraps = nb.scraps.data_dict
                if 'hide' in scraps and scraps['hide'] == True:
                    continue
                subprocess.run(f'jupyter nbconvert {out_nb_path} --no-input --to html', shell=True, check=True)
                dest_name = Path(rpt_nb_path.name).with_suffix('.html')
                dest_path = rpt_output_path / str(param_val) / dest_name
                dest_path.parent.mkdir(parents=True, exist_ok=True)
                out_html_path.replace(dest_path)
                rpts.append(
                    {
                        'title': scraps['title'],
                        'sort_order': scraps['sort_order'],
                        'file_name': str(dest_name),
                    }
                )
                completed_ct += 1
            except pm.PapermillExecutionError as err:
                aborted_ct += 1
                if err.ename == 'RuntimeError':
                    logging.info(f'Report aborted for server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}: {err.evalue}')
                else:
                    logging.exception(f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}')
            except:
                aborted_ct += 1
                logging.exception(f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}')
        rpts = sorted(rpts, key=lambda r: (r['sort_order'], r['title']))
        if rpts:
            report_dict[param_val] = rpts
    logging.info(f'For server {server_domain}, report type {param_name}, {completed_ct} reports completed, {aborted_ct} reports aborted.')
    return report_dict
def run_report_set ( server_url : str , param_name : str , param_values : Iterable , working_path : Path , nb_template_path : Path , rpt_output_path : Path , ) : server_domain = urlparse ( server_url ) . netloc report_dict = { } out_nb_path = working_path / 'report.ipynb' out_html_path = working_path / 'report.html' completed_ct = 0 aborted_ct = 0 for param_val in param_values : rpts = [ ] for rpt_nb_path in nb_template_path . glob ( '*.ipynb' ) : try : pm . execute_notebook ( str ( rpt_nb_path ) , str ( out_nb_path ) , parameters = { 'server_web_address' : server_url , param_name : param_val } , kernel_name = 'python3' , ) nb = sb . read_notebook ( str ( out_nb_path ) ) scraps = nb . scraps . data_dict if 'hide' in scraps and scraps [ 'hide' ] == True : continue subprocess . run ( f'jupyter nbconvert {out_nb_path} --no-input --to html' , shell = True , check = True ) dest_name = Path ( rpt_nb_path . name ) . with_suffix ( '.html' ) dest_path = rpt_output_path / str ( param_val ) / dest_name dest_path . parent . mkdir ( parents = True , exist_ok = True ) out_html_path . replace ( dest_path ) rpts . append ( { 'title' : scraps [ 'title' ] , 'sort_order' : scraps [ 'sort_order' ] , 'file_name' : str ( dest_name ) , } ) completed_ct += 1 except pm . PapermillExecutionError as err : aborted_ct += 1 if err . ename == 'RuntimeError' : logging . info ( f'Report aborted for server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}: {err.evalue}' ) else : logging . exception ( f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}' ) except : aborted_ct += 1 logging . exception ( f'Error processing server={server_domain}, {param_name}={param_val}, report={rpt_nb_path.name}' ) rpts = sorted ( rpts , key = lambda r : ( r [ 'sort_order' ] , r [ 'title' ] ) ) if rpts : report_dict [ param_val ] = rpts logging . info ( f'For server {server_domain}, report type {param_name}, {completed_ct} reports completed, {aborted_ct} reports aborted.' ) return report_dict
Creates a set of reports by cycling through a set of buildings or organizations
and then cycling through a set of Jupyter notebook templates used to create the reports.
Creates a set of reports by cycling through a set of buildings or organizations and then cycling through a set of Jupyter notebook templates used to create the reports .
"""Creates a set of reports by cycling through a set of buildings or organizations
    and then cycling through a set of Jupyter notebook templates used to create the reports.
    Input Parameters:
        server_url: the full URL to BMON server that holds the data
        param_name: the name of building or organization parameter in the notebook template
        param_values: a list of values to cycle through for the notebook parameter.
        working_path: the Path to a working directory for use by this routine
        nb_template_path: a Path to the directory where the template report notebooks are stored.
        rpt_output_path: a Path to the directory where the final HTML reports are stored. A
            subdirectory for each parameter value will be created in this directory to hold all
            the reports for that particular building or organization.

    Returns a dictionary keyed on parameter value that lists the reports for that
    parameter value; each item in the list is a dictionary describing the report.
    """ # extract server domain for message labeling purposes # Make a dictionary to keep track of the reports created for each parameter # value.  Key is paramater value, value is list of report records.  Each report # record is a dictionary giving info about the report. # Make file names for the calculate notebook being currently run and  # the HTML report that is created from that template. # Keep track of how many reports completed and aborted # Loop through all the parameter values (e.g. buildings or organizations) # a list of reports for this parameter value # get the glued scraps from the notebook # report is not available, probably due to lack of data # convert the notebook to html. throw an error if one occurs. # move the resulting html report to the report directory # first create the destination file name and create the necessary # directories, if they don't exist. # Add to the dictionary of completed reports. # This error was raised intentionally to stop notebook execution. # Just log an info message. # sort the reports in sort order for this parameter value # only include if reports are present. # log the number of completed and aborted reports
server_url
str
param_name
str
param_values
Iterable
working_path
Path
nb_template_path
Path
rpt_output_path
Path