Skip to content

Commit

Permalink
Handle line breaks in model variable description (#710)
Browse files Browse the repository at this point in the history
fixes #703
  • Loading branch information
t-sommer authored Nov 20, 2024
1 parent a710177 commit 4306404
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions fmpy/gui/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,15 +1322,15 @@ 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

filename, ext = os.path.splitext(self.filename)

filename, _ = QFileDialog.getSaveFileName(
parent=self,
directory=filename + '.ipynb',
dir=f'{filename}.ipynb',
filter='Jupyter Notebooks (*.ipynb);;All Files (*)'
)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion fmpy/gui/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions fmpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("'", "\\'")
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 4306404

Please sign in to comment.