Graphical front-end¶
A small Streamlit application is included as an
optional extra. It offers buttons, drop-down menus and editable tables for
the .model language instead of a bare text file: you describe the model,
the application compiles it while you edit and shows
the generated Fortran module (ready to download), and
a typeset preview of the function and of every derivative.
Installation¶
The GUI is kept out of the default installation. Install it with:
pip install 'thermodiff[gui]'
Usage¶
Start the application with:
thermodiff-gui
Your browser opens on a form with the model name, the Fortran use
statements and editable tables for the variables, the parameters and the
intermediate expressions. A drop-down menu loads the bundled examples
(Peng-Robinson, ideal gas enthalpy, mole fraction and a minimal template) and
a button downloads the generated .f90 file.
Errors are reported with the offending line of the assembled .model
file, so the strict validation of the command line compiler is available
here too.
Python API¶
The widgets live in thermodiff.gui.app, which is a Streamlit script.
Everything that can be used or tested without a running Streamlit server is
collected in thermodiff.gui.logic:
Streamlit independent logic behind the model GUI.
Everything in this module is plain Python: the GUI only wires widgets to
these helpers. Keeping it free of any Streamlit import means the helpers
can be unit tested without installing the optional gui extra.
- DEFAULT_PRESET = 'standard'¶
Preset selected by default in the GUI.
- PRESET_HELP = {'fastest': 'Fastest generated code; slower generation, last-bit differences.', 'minimal': 'Plain generator output, no extra optimisations.', 'standard': 'Recommended: chain rule, symmetry and sharing.'}¶
One-line description of each code-generation preset.
- assemble_model(model_name, uses, variables, parameters, expressions, function)[source]¶
Build the text of a
.modelfile from structured parts.- Parameters:
model_name (str) – Name of the model. Empty falls back to
model.uses (list of str) –
usestatements, without the leading keyword.variables (list of dict) – Rows with
nameanddimskeys.parameters (list of dict) – Rows with
nameanddimskeys.expressions (list of dict) – Rows with
lhsandrhskeys.function (str) – Body of the
functionsection.
- Returns:
The assembled
.modeltext.- Return type:
str
- build_from_parts(model_name, uses, variables, parameters, expressions, function, optional_derivatives=False, preset='standard')[source]¶
Assemble structured parts and compile them in one step.
- Parameters:
model_name – See
assemble_model().uses – See
assemble_model().variables – See
assemble_model().parameters – See
assemble_model().expressions – See
assemble_model().function – See
assemble_model().optional_derivatives (bool, optional) – See
build_result().preset (str, optional) – See
build_result().
- Returns:
The
build_result()dictionary.- Return type:
dict
- build_result(text, filename='<editor>', optional_derivatives=False, preset='standard')[source]¶
Compile
.modeltext and collect everything the GUI shows.This never raises: compilation problems are reported in the
errorentry of the result.- Parameters:
text (str) – Contents of the
.modelfile.filename (str, optional) – Name used in error messages.
optional_derivatives (bool, optional) – Declare the outputs as Fortran
optionalarguments so the generated subroutine computes only the derivatives the caller passes.preset ({"minimal", "standard", "fastest"}, optional) – Code-generation preset, see
PRESET_HELP.
- Returns:
With keys
text,code(Fortran orNone),latex(preview orNone),warningsanderror.- Return type:
dict
- parse_parts(text)[source]¶
Split
.modeltext back into GUI friendly rows.This is the inverse of
assemble_model(), used to load the bundled examples into the editor widgets.- Parameters:
text (str) – Contents of a
.modelfile.- Returns:
With keys
model_name,uses,variables,parameters,expressionsandfunction. The keys match the parameters ofassemble_model(), soassemble_model(**parts)rebuilds the text.- Return type:
dict
- Raises:
ModelError – If the text is malformed and cannot be split into sections.
- annotated_source(text, line=None)[source]¶
Return
textwith line numbers, markinglinewith an arrow.- Parameters:
text (str) – Source text.
line (int, optional) – Line number to highlight.
- Returns:
A single string ready to be shown in a code block.
- Return type:
str
- error_context(error, text)[source]¶
Return the source line a compilation error points at.
- Parameters:
error (dict) – The
errorentry of abuild_result()dictionary.text (str) – Source text the error refers to.
- Returns:
The offending line, without its line number, or
Nonewhen the error has no location.- Return type:
str or None