From 4306404c7b92f1ba1e7cc0d421f25048c49e5142 Mon Sep 17 00:00:00 2001 From: Torsten Sommer Date: Wed, 20 Nov 2024 17:30:36 +0100 Subject: [PATCH] Handle line breaks in model variable description (#710) fixes #703 --- fmpy/gui/MainWindow.py | 6 +++--- fmpy/gui/model.py | 3 ++- fmpy/util.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/fmpy/gui/MainWindow.py b/fmpy/gui/MainWindow.py index 9e388b97..d439ebfd 100644 --- a/fmpy/gui/MainWindow.py +++ b/fmpy/gui/MainWindow.py @@ -1322,7 +1322,7 @@ def removeSourceCode(self): self.load(self.filename) def createJupyterNotebook(self): - """ Create a Juypyter Notebook to simulate the FMU """ + """ Create a Jupyter Notebook to simulate the FMU """ from fmpy.util import create_jupyter_notebook @@ -1330,7 +1330,7 @@ def createJupyterNotebook(self): filename, _ = QFileDialog.getSaveFileName( parent=self, - directory=filename + '.ipynb', + dir=f'{filename}.ipynb', filter='Jupyter Notebooks (*.ipynb);;All Files (*)' ) @@ -1358,7 +1358,7 @@ def createCMakeProject(self): project_dir = QFileDialog.getExistingDirectory( parent=self, caption='Select CMake Project Folder', - directory=os.path.dirname(self.filename)) + dir=os.path.dirname(self.filename)) if project_dir: create_cmake_project(self.filename, project_dir) diff --git a/fmpy/gui/model.py b/fmpy/gui/model.py index 5443e58c..578e9a31 100644 --- a/fmpy/gui/model.py +++ b/fmpy/gui/model.py @@ -126,7 +126,8 @@ def columnData(self, v, column, role): elif v.declaredType is not None and v.declaredType.unit is not None: return v.declaredType.unit elif column == 'Description': - return v.description + if v.description is not None: + return ' '.join(v.description.splitlines()) return None diff --git a/fmpy/util.py b/fmpy/util.py index 2f6d2b3d..02fb93eb 100644 --- a/fmpy/util.py +++ b/fmpy/util.py @@ -1232,10 +1232,14 @@ def create_jupyter_notebook(filename, notebook_filename=None): output_variables.append((variable.name, variable.description)) for variable in model_description.modelVariables: + if variable.causality == 'parameter' and variable.variability in ['fixed', 'tunable']: name, start, unit, description = variable.name, variable.start, variable.unit, variable.description + if description: + description = ' '.join(description.splitlines()) + def fix(v): if variable.type == 'String': return f"'%s'" % v.replace("'", "\\'") @@ -1252,9 +1256,11 @@ def fix(v): if unit is None and variable.declaredType is not None: unit = variable.declaredType.unit + max_name = max(max_name, len(name)) max_start = max(max_start, len(start)) max_unit = max(max_unit, len(unit)) if unit else max_unit + parameters.append((name, start, unit, description)) code = "import fmpy\n" @@ -1279,7 +1285,7 @@ def fix(v): else: code += start.rjust(max_start + 1) + "," + " " * (max_unit + 5) if description: - code += " # " + description + code += " # " + ' '.join(description.splitlines()) code += "\n" code += "}\n" @@ -1288,7 +1294,7 @@ def fix(v): for name, description in output_variables: code += " " + ("'%s'," % name.replace("'", "\\'")).ljust(max_output + 3) if description: - code += " # " + description + code += " # " + ' '.join(description.splitlines()) code += "\n" code += "]\n" code += "\n"