-
Notifications
You must be signed in to change notification settings - Fork 7
render math in job tab info #992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: protos
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question - we may be able to get away with transparency and white font when in dark mode. |
oerc0122
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the MathRenderer can be simplified by using REs to their full extent. Though you may have to be careful and make a copy of the text since replace may disrupt finditer.
| from matplotlib import pyplot as plt | ||
|
|
||
|
|
||
| class MathRenderer: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, this would be an ideal use-case of 3.14's t-strings
| def processInlineExpressions(text: str) -> list[tuple[str, bool]]: | ||
| pattern = r"(:math:`.*?`)" | ||
| substrings = re.split(pattern, text) | ||
| scanned = [] | ||
| for s in substrings: | ||
| if s.startswith(":math:"): | ||
| expr = s[len(":math:`") : -1] | ||
| scanned.append((expr, True)) | ||
| else: | ||
| scanned.append((s, False)) | ||
|
|
||
| return scanned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a somewhat strange way to do this. The usual way would be to use Regular expression groups.
| def processInlineExpressions(text: str) -> list[tuple[str, bool]]: | |
| pattern = r"(:math:`.*?`)" | |
| substrings = re.split(pattern, text) | |
| scanned = [] | |
| for s in substrings: | |
| if s.startswith(":math:"): | |
| expr = s[len(":math:`") : -1] | |
| scanned.append((expr, True)) | |
| else: | |
| scanned.append((s, False)) | |
| return scanned | |
| def processInlineExpressions(text: str) -> list[tuple[str, bool]]: | |
| pattern = r"(:math:`(.*?)`)" # <-- Should really be class-level constant | |
| last = 0 | |
| for match in re.finditer(pattern, text): | |
| scanned.append((text[last:match.start], False)) | |
| scanned.append((match.group(1), True)) | |
| last = match.end | |
| scanned.append((text[last:], False)) | |
| return scanned |
Although I'm still not sure whether you really want to capture the non-converting bits or whether you just want to substitute the matches in-place for their image equivalents, which simplifies the logic massively.
| def processInlineExpressions(text: str) -> list[tuple[str, bool]]: | |
| pattern = r"(:math:`.*?`)" | |
| substrings = re.split(pattern, text) | |
| scanned = [] | |
| for s in substrings: | |
| if s.startswith(":math:"): | |
| expr = s[len(":math:`") : -1] | |
| scanned.append((expr, True)) | |
| else: | |
| scanned.append((s, False)) | |
| return scanned | |
| def processInlineExpressions(self, text: str) -> None: | |
| pattern = r"(:math:`(.*?)`)" # <-- Should really be class-level constant | |
| for match in re.finditer(pattern, text): | |
| text.replace(match[0], self.render(match[1])) |
| @staticmethod | ||
| def processMultiLineBlockExpression( | ||
| strings: list[str], n: int | ||
| ) -> tuple[list[tuple[str, bool]], int]: | ||
| substrings = strings[n + 1 :] | ||
| group = [] | ||
| for s in substrings: | ||
| if (not s) or (s == r"<br /><br />"): | ||
| break | ||
| group.append(s) | ||
|
|
||
| group = [s.replace(r"<br />", "") for s in group] | ||
| total = "".join(group) | ||
| if total.startswith(r"<br />") and total.endswith(r"<br />"): | ||
| result = total.split(r"<br />")[1] | ||
| else: | ||
| result = total.split(r"<br /><br />")[0] | ||
|
|
||
| return [(result, True)], n + len(group) + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you can use pattern matching here.
| @staticmethod | |
| def processMultiLineBlockExpression( | |
| strings: list[str], n: int | |
| ) -> tuple[list[tuple[str, bool]], int]: | |
| substrings = strings[n + 1 :] | |
| group = [] | |
| for s in substrings: | |
| if (not s) or (s == r"<br /><br />"): | |
| break | |
| group.append(s) | |
| group = [s.replace(r"<br />", "") for s in group] | |
| total = "".join(group) | |
| if total.startswith(r"<br />") and total.endswith(r"<br />"): | |
| result = total.split(r"<br />")[1] | |
| else: | |
| result = total.split(r"<br /><br />")[0] | |
| return [(result, True)], n + len(group) + 1 | |
| def processMultiLineBlockExpression( | |
| self, strings: str | |
| ) -> None: | |
| pattern = r".. math::\s*\n(?:\s*\n)?(\s+)(.+)\n(\1.+\n)*" # <- Should be class level variable | |
| for match in re.finditer(pattern, text): | |
| if match[3] is not None: | |
| body = "".join(more_itertools.prepend(match[2], match[3].splitlines())) | |
| else: | |
| body = match[2] | |
| text.replace(match[0], self.render(body)) |
| @staticmethod | ||
| def processBlockExpression(text: str) -> list[tuple[str, bool]]: | ||
| return [(text[len(".. math:: ") :], True)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, we can use pattern matching:
| @staticmethod | |
| def processBlockExpression(text: str) -> list[tuple[str, bool]]: | |
| return [(text[len(".. math:: ") :], True)] | |
| def processBlockExpression(text: str) -> list[tuple[str, bool]]: | |
| pattern = ".. math::\s*(.+)" | |
| for match in re.finditer(pattern, text): | |
| text.replace(match[0], self.render(match[1])) |
| for token, is_expression in scanned: | ||
| if is_expression: | ||
| image = MathRenderer.from_cache(token) | ||
| if len(token) < 10: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bear in mind that latex tokens may not correspond to large images:
\underbrace{\mathbf{x}} = \mathbf{3}
|
I don't think this needs to be fix here but something I discover in #1038 which also will happen for the equation images. |
|
A few more things. Some text has been dropped. The "for a set of atoms" is missing. Also the equations are on their own line, I think it would be nice if you could inline them.
here is the text on protos.
@RobBuchananCompPhys Can you resolve the merge conflict, I can have a look to see if dark/light mode change can be fixed for the equation images. |









Description of work
Currently, the Job tab info does not render the embedded LaTex expression strings, which can look less informative and cluttered. This PR proposes an enhancement in which images (rendered in a simple
matplotlibfigure) of the expression are inserted into the html of the Job tab, allowing mathematical expressions to be displayed in the text.Fixes

Before:
After:

To test
Please describe the tests that were run to verify the changes.