You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The presence of \n in equation objects is common due to how the text is formatted in the original IODE GUI and associated source files (e.g., for multiline display purposes). When this representation is brought into Python interactive mode, the \n characters can (slightly) disrupt readability.
°°°°°°°°
First attempt:
Ideally, when working in an interactive console, we want the expression io.equations["ACAF"].lec to no longer display a hard-coded \n in the output, similar to how an ordinary print() statement would handle the situation. Unfortunately, we see:
A possible solution introduces a custom string class FormattedLecString that overrides the __repr__ method to return the string just as if it were an ordinary print statement.
This approach has the potential downside that the return objects are no longer of the standard string type, which might cause issues when assigning or using them in situations where str is expected.
°°°°°°°° Second attempt:
Another approach is to modify the behavior of the interactive console itself. First, we need to determine whether we are in interactive mode, which can be done using this method. There are ways to customize the standard Python REPL but unfortunately they do not have an effect within the usual IPython environment. Extra adjustments are therefore needed to get things working in PyCharm or VSCode interactive mode.
importsysfromIPythonimportget_ipython# Execute *only* next two lines in interactive mode (e.g., PyCharm console, VSCode Jupyter/Interactive, etc.)var="abc\ndef"var# Next, continue and execute the code below in interactive mode and observe the new behavior.defsetup_custom_display_interactive_mode():
ifhasattr(sys, 'ps1'):
print("interactive mode")
# Interactive Mode Type 1: custom display hook for standard Python REPLdefmy_displayhook(value):
ifvalueisnotNone:
print(valueifisinstance(value, str) elserepr(value))
# Assign custom hook to sys.displayhooksys.displayhook=my_displayhook# Interactive Mode Type 2: check if running IPython and set up custom display functionip=get_ipython()
ifipisnotNone:
defcustom_display_string(value, p, cycle): # keep 'cycle' argumentp.text(valueifisinstance(value, str) elserepr(value))
ip.display_formatter.formatters['text/plain'].for_type(str, custom_display_string)
else:
print("non-interactive mode")
setup_custom_display_interactive_mode()
var
The output and behaviour is as follows:
In [3]: var
Out[3]: 'abc\ndef'
...
In [5] var
Out[5]:
abc
def
If there are better implementations or solutions, please let me know (@alixdamman, @gdementen). It might also be worth discussing whether implementing this feature is sensible in the first place (taking into account user-experience, maintenance cost, performance, etc.).
The text was updated successfully, but these errors were encountered:
Problem:
The presence of
\n
in equation objects is common due to how the text is formatted in the original IODE GUI and associated source files (e.g., for multiline display purposes). When this representation is brought into Python interactive mode, the\n
characters can (slightly) disrupt readability.°°°°°°°°
First attempt:
Ideally, when working in an interactive console, we want the expression
io.equations["ACAF"].lec
to no longer display a hard-coded\n
in the output, similar to how an ordinaryprint()
statement would handle the situation. Unfortunately, we see:A possible solution introduces a custom string class
FormattedLecString
that overrides the__repr__
method to return the string just as if it were an ordinary print statement.This approach has the potential downside that the return objects are no longer of the standard string type, which might cause issues when assigning or using them in situations where
str
is expected.°°°°°°°°
Second attempt:
Another approach is to modify the behavior of the interactive console itself. First, we need to determine whether we are in interactive mode, which can be done using this method. There are ways to customize the standard Python REPL but unfortunately they do not have an effect within the usual IPython environment. Extra adjustments are therefore needed to get things working in PyCharm or VSCode interactive mode.
The output and behaviour is as follows:
If there are better implementations or solutions, please let me know (@alixdamman, @gdementen). It might also be worth discussing whether implementing this feature is sensible in the first place (taking into account user-experience, maintenance cost, performance, etc.).
The text was updated successfully, but these errors were encountered: