Skip to content

Commit

Permalink
html formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Nov 18, 2024
1 parent 2b9f8a7 commit ec22bc2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 77 deletions.
117 changes: 48 additions & 69 deletions src/easyreflectometry/summary/html_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,45 @@
<html>
<style>
th, td {
padding-right: 18px;
}
th {
text-align: left;
}
</style>
<body>
<table>
<tr></tr>
<!-- Summary title -->
<tr>
<td><h1>Summary</h1></td>
</tr>
<tr></tr>
<!-- Project -->
project_information_section
<tr></tr>
<!-- Sample -->
<tr>
<td><h3>Sample</h3></td>
</tr>
<tr></tr>
sample_section
<tr></tr>
<!-- Experiments -->
<tr>
<td><h3>Experiments</h3></td>
</tr>
<tr></tr>
experiments_section
<tr></tr>
<!-- Analysis -->
refinement_section
</table>
</body>
<style>
th, td {
padding-right: 18px;
}
th {
text-align: left;
}
</style>
<body>
<table>
<!-- Summary title -->
<tr>
<td><h1>Summary</h1></td>
</tr>
<!-- Project -->
project_information_section
<!-- Sample -->
<tr>
<td><h3>Sample</h3></td>
</tr>
sample_section
<!-- Experiments -->
<tr>
<td><h3>Experiments</h3></td>
</tr>
experiments_section
<!-- Analysis -->
<tr>
<td><h3>Refinement</h3></td>
</tr>
refinement_section
</table>
</body>
</html>"""


Expand All @@ -71,8 +49,6 @@
<td><h3>Project information</h3></td>
</tr>
<tr></tr>
<tr>
<th>Title</th>
<th>project_title</th>
Expand All @@ -87,6 +63,15 @@
</tr>
"""

HTML_PARAMETER_HEADER_TEMPLATE = """
<tr>
<th>parameter_name</th>
<th>parameter_value</th>
<th>parameter_unit</th>
<th>parameter_error</th>
</tr>
"""

HTML_PARAMETER_TEMPLATE = """
<tr>
<td>parameter_name</td>
Expand Down Expand Up @@ -120,12 +105,6 @@
"""

HTML_REFINEMENT_TEMPLATE = """
<tr>
<td><h3>Refinement</h3></td>
</tr>
<tr></tr>
<tr>
<td>Calculation engine</td>
<td>calculation_engine</td>
Expand Down
21 changes: 13 additions & 8 deletions src/easyreflectometry/summary/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from easyreflectometry.utils import count_free_parameters

from .html_templates import HTML_DATA_COLLECTION_TEMPLATE
from .html_templates import HTML_PARAMETER_HEADER_TEMPLATE
from .html_templates import HTML_PARAMETER_TEMPLATE
from .html_templates import HTML_PROJECT_INFORMATION_TEMPLATE
from .html_templates import HTML_REFINEMENT_TEMPLATE
Expand All @@ -17,20 +18,25 @@ def compile_html_summary(self):
html = HTML_TEMPLATE
project_information_section = self._project_information_section()
if project_information_section == '': # no project information
project_information_section = '<tr><td>No project information</td></tr>'
project_information_section = '<td>No project information</td>'
html = html.replace('project_information_section', project_information_section)

html = html.replace('sample_section', self._sample_section())

experiments_section = self._experiments_section()
if experiments_section == '': # no experiments
experiments_section = '<tr><td>No experiments</td></tr>'
experiments_section = '<td>No experiments</td>'
html = html.replace('experiments_section', experiments_section)

html = html.replace('refinement_section', self._refinement_section())

return html

def save_html_summary(self, filename: str) -> None:
html = self.compile_html_summary()
with open(filename, 'w') as f:
f.write(html)

def _project_information_section(self) -> None:
html_project = ''
if self._project.created:
Expand All @@ -46,11 +52,11 @@ def _project_information_section(self) -> None:
def _sample_section(self) -> None:
html_parameters = []

html_parameter = HTML_PARAMETER_TEMPLATE
html_parameter = html_parameter.replace('parameter_name', '<th>Name</th>')
html_parameter = html_parameter.replace('parameter_value', '<th>Value</th>')
html_parameter = html_parameter.replace('parameter_unit', '<th>Unit</th>')
html_parameter = html_parameter.replace('parameter_error', '<th>Error</th>')
html_parameter = HTML_PARAMETER_HEADER_TEMPLATE
html_parameter = html_parameter.replace('parameter_name', 'Name')
html_parameter = html_parameter.replace('parameter_value', 'Value')
html_parameter = html_parameter.replace('parameter_unit', 'Unit')
html_parameter = html_parameter.replace('parameter_error', 'Error')
html_parameters.append(html_parameter)

for parameter in self._project.parameters:
Expand Down Expand Up @@ -87,7 +93,6 @@ def _sample_section(self) -> None:
# html_phase = html_phase.replace('angle_beta', f'{angle_beta}')
# html_phase = html_phase.replace('angle_gamma', f'{angle_gamma}')

html_parameters.append('/n<tr></tr>')
html_parameters_str = '\n'.join(html_parameters)

return html_parameters_str
Expand Down
14 changes: 14 additions & 0 deletions tests/summary/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def test_compile_html_summary(self, project: Project) -> None:
assert 'experiments results html' in result
assert 'refinement result html' in result

def test_save_html_summary(self, project: Project, tmp_path) -> None:
# When
project._created = True
summary = Summary(project)
# summary.compile_html_summary = MagicMock(return_value='html')

# Then
summary.save_html_summary(tmp_path / 'filename.html')

# Expect
assert os.path.exists(tmp_path / 'filename.html')
# with open('filename', 'r') as f:
# assert f.read() == 'html'

def test_project_information_section(self, project: Project) -> None:
# When
project._created = True
Expand Down

0 comments on commit ec22bc2

Please sign in to comment.