htmlpp.py#

Define the HtmlPreprocessor class to generate a single HTML file from many source files.

class openmdao.visualization.htmlpp.HtmlPreprocessor(start_filename: str, output_filename: str, search_path=[], allow_overwrite=False, var_dict: dict = None, json_dumps_default=None, verbose=False)[source]

Bases: object

Recursively substitute and insert source files to produce a single HTML file.

Source files contain text with directives in the form: <<hpp_directive arg [flag1] [flag2]>>

Parameters:
start_filenamestr

The file to begin processing from.

output_filenamestr

The path to the newly merged HTML file.

search_pathstr[]

List of directory names to search for files.

allow_overwritebool

If true, overwrite the output file if it exists.

var_dictdict

Dictionary of variable names and values referenced by hpp_pyvar directives.

json_dumps_defaultfunction

Passed to json.dumps() as the “default” parameter that gets called for objects that can’t be serialized.

verbosebool

If True, print some status messages to stdout.

Notes

Recognized Directives:

<<hpp_insert path/to/file [compress] [dup]>>

Paste path/to/file verbatim into the text.

<<hpp_script path/to/script [dup]>>

Insert path/to/script between script tags.

<<hpp_style path/to/css [dup]>>

Insert path/to/css between style tags.

<<hpp_bin2b64 path/to/file [dup]>>

Convert a binary file to a b64 string and insert it.

<<hpp_pyvar variable_name [compress]>>

Insert the string value of the named Python variable. If the referenced variable is non-primitive, it’s converted to JSON.

Flags:

compress :

The replacement content will be compressed and converted to a base64 string. It’s up to the JavaScript code to decode and uncompress it.

dup :

If a file has already been included once, it will be ignored on subsequent inclusions unless the dup flag is used.

Commented directives (//, /* */, or <!-- -->) will replace the entire comment. When a directive is commented, it can only be on a single line or the comment-ending characters will not be replaced.

All paths in the directives are relative to the directory that the start file is located in (or if start_path was specified) unless it is absolute.

Nothing is written until every directive has been successfully processed.

Attributes:
_start_pathPath

A Path object to help with testing functions for the provided start_filename.

_start_filenamestr

The path to the file to begin processing from, normally an HTML file.

_search_pathPath

List of Path objects to search for included files.

_output_filenamestr

The path to the newly merged HTML file.

_var_dictdict

Dictionary of variable names and values referenced by hpp_pyvar directives.

_allow_overwritebool

If true, overwrite the output file if it exists.

_verbosebool

If True, print some status messages to stdout.

_loaded_filenameslist

List of filenames loaded so far, to determine if a file has been already loaded.

_json_dumps_defaultfunction

Passed to json.dumps() as the “default” parameter that gets called for objects that can’t be serialized.

__init__(start_filename: str, output_filename: str, search_path=[], allow_overwrite=False, var_dict: dict = None, json_dumps_default=None, verbose=False)[source]

Configure the preprocessor and validate file paths.

find_file(filename: str) str[source]

Check specified locations for the provided filename.

Parameters:
filenamestr

The path to the text file to locate.

Returns:
str

The full path to the existing file if found.

load_file(filename: str, rlvl=0, binary=False, allow_dup=False) str[source]

Open and read the specified text file.

Parameters:
filenamestr

The path to the text file to read.

rlvlint

Recursion level to help with indentation when verbose is enabled.

binarybool

True if the file is to be opened in binary mode and converted to a base64 str.

allow_dupbool

If False, return an empty string for a filename that’s been previously loaded.

Returns:
str

The complete contents of the file.

msg(msg: str, rlvl=0)[source]

Print a message to stdout if self.verbose is True.

Parameters:
msgstr

The message to print.

rlvlint

Recursion level to help with indentation when verbose is enabled.

parse_contents(contents: str, rlvl=0) str[source]

Find the preprocessor directives in the file and replace them with the desired content.

Will recurse if directives are also found in the new content.

Parameters:
contentsstr

The contents of a preloaded text file.

rlvlint

Recursion level to help with indentation when verbose is enabled.

Returns:
str

The complete contents represented as a base64 string.

run() None[source]

Initialize the preprocessor, then save the result as a new file.