-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: Auto-detect interactive mode #4688
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
Conversation
Grr, the pep8 build won't install... |
👍 assuming this works reliably. We should document somewhere what None means. We also need to update matplotlibrc.template to reflect the change. |
The pep8 issue was fixed by #4687 |
Hehe, good plan, I will update I spent a long time trying to figure out how this "interactive mode" worked, looking through the examples, trying different combinations, until finally I stumbled across the FAQ. I think I intuitively expected it to work like this, then a quick search online and quickly found this SO solution |
Re documentation that sounds sensible. I would also add a comment to both rcsetup and matplotlibrc.template explaining what None means. (Hopefully the Traits effort will mean that we don't have to duplicate information to much longer 😃 ) |
ret = rcParams['interactive'] | ||
if ret is None: | ||
ret = not hasattr(main, '__file__') | ||
return ret |
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.
To maintain the current returns change this to return bool(ret)
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.
What do you mean? This should always return a bool
as hasattr
returns a bool
.
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.
a None
can fall through
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.
ah, never mind
21:40 $ cat test.py
print('hi mom')
import __main__ as main
print(__file__)
print(main.__file__)
def is_interactive():
'Return true if plot mode is interactive'
ret = None
if ret is None:
ret = not hasattr(main, '__file__')
return ret
print(is_interactive()) Yields 21:26 $ python -i test.py
hi mom
test.py
test.py
False
>>> import test
hi mom
/tmp/test.pyc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/test.py", line 4, in <module>
print(main.__file__)
AttributeError: 'module' object has no attribute '__file__'
>>> main
<module '__main__' (built-in)>
>>> main.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> is_interactive()
True We got pretty badly burned by something like this last release cycle when we looked at PS1 to do something similar and broke things pretty badly for people who were working with embedded python interpreters. |
attn @mdboom |
As far as I see it, it works as expected above, I thought about adding a check to for the Anyway, pretty nice feature for just 4 extra lines (and 2 changed lines) of code. |
Consider import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(3))
plt.show() If you run this with |
This hasn't been responded to for a long time so I'm closing. Feel free to reopen... |
Makes rcParam['interactive'] a tri-state, with a value of
None
indicating that it should take the value from the environment. Note python only switches to interactive mode after executing any pre-given commands.