-
Notifications
You must be signed in to change notification settings - Fork 2
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
Defer formatting to adjust format based on the ouput context #114
Comments
This change may likely remove the |
edit: I read the suggestion more carefully. One issue with this pattern is that right now the output of the
Would this still work as expected? Does passing an object into an f string formatter always cast it to a string? What about
|
Yes, using f-strings would cast the result as a string. You might be onto something with the |
@Batalex ok, I've thought about this a little bit. The main thing that I like about it is that it provides hooks for However, I don't think those things require deferred formatting. Consider
Of course the So @Batalex, does this realize the main thrust of this recommendation? Or was the deferred formatting actually critical to the suggestion? |
I proposed the lazy formatting so that special characters would be rendered appropriately:
Your suggestion above could work; it seems more complex to replace those characters afterward compared to the following approach class FormattedValue(str):
def __init__(self, fmt: Formatter, value: Number, uncertainty: Optional[Number] = None, /) -> None:
self.fmt = fmt
self.value = value
self.uncertainty = uncertainty
def __str__(self) -> str:
rendered_options = self.fmt.user_options.render()
if self.uncertainty is None:
return format_num(Decimal(str(self.value)), rendered_options)
else:
return format_val_unc(Decimal(str(self.value)),
Decimal(str(self.uncertainty)),
rendered_options)
def _repr_html_(self):
rendered_options = self.fmt.user_options.render()
rendered_options.html = True
if self.uncertainty is None:
return format_num(Decimal(str(self.value)), rendered_options)
else:
return format_val_unc(Decimal(str(self.value)),
Decimal(str(self.uncertainty)),
rendered_options)
def _repr_latex_(self):
rendered_options = self.fmt.user_options.render()
rendered_options.latex = True
if self.uncertainty is None:
return format_num(Decimal(str(self.value)), rendered_options)
else:
return format_val_unc(Decimal(str(self.value)),
Decimal(str(self.uncertainty)),
rendered_options)
(without all the copy paste) |
@Batalex see #134. I've come up with much cleaner way to generate latex compatible strings post-facto. I just had to hit my head against some regexes and string replacements. With this I'm tentatively deciding to drop the This also made it very easy to implement a I'm somehow comfortable having the latex and HTML conversion be outside the scope of the formatter. the @Batalex are you aware of other tools (other than IPython/jupyter) that would hook into |
Wow that's perfect!
Yes, I would love to see how this fits into quarto |
Quarto works somewhat as expected. I have this jupyter notebook cell (the same as appears in the latest docs that looks like I'm curious how people want to use the Should probably open another issue about this. |
Closed by #134 |
Discussed in #83
Originally posted by Batalex November 20, 2023
The current
sciform
implementation eagerly formats values as soon asFormatter.__call__
is called.This discussion is about making
sciform
implementation lazy so the actual format would happen when the value is printed.Here is a quick look at what it could look like
The example above would be similar to the current implementation, but you could then add
_repr_html_
or_repr_latex_
methods so that your displayed value uses\textsuperscript
in a LaTeX document or<sup></sup>
in a notebook.The text was updated successfully, but these errors were encountered: