Skip to content

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

Closed

Conversation

OceanWolf
Copy link
Member

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.

@OceanWolf OceanWolf changed the title Auto-detect interactive mode ENH: Auto-detect interactive mode Jul 14, 2015
@OceanWolf
Copy link
Member Author

Grr, the pep8 build won't install...

@tacaswell tacaswell added this to the proposed next point release milestone Jul 14, 2015
@jenshnielsen
Copy link
Member

👍 assuming this works reliably. We should document somewhere what None means. We also need to update matplotlibrc.template to reflect the change.

@jenshnielsen
Copy link
Member

The pep8 issue was fixed by #4687

@OceanWolf
Copy link
Member Author

Hehe, good plan, I will update matplotlibrc.template and document it later this week (faq, what's new, anywhere else?), really we need a beginners guide/tutorial in which we can incorporate the examples into, but too big a project for now 😉.

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

@jenshnielsen
Copy link
Member

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
Copy link
Member

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)

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, never mind

@tacaswell
Copy link
Member

__main__ is kind of magic:

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.

@tacaswell
Copy link
Member

attn @mdboom

@OceanWolf
Copy link
Member Author

As far as I see it, it works as expected above, is_interactive, it gives False while python exists in non-interactive mode, i.e. while it still executes the initial file, then gives True after that...

I thought about adding a check to for the -i parameter passed to python, but thought that could wait until later... perhaps I should look into it.

Anyway, pretty nice feature for just 4 extra lines (and 2 changed lines) of code.

@tacaswell
Copy link
Member

Consider

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(3))

plt.show()

If you run this with python -i it blocks until you close the window. If you comment out plt.show() and do the same plt.show() behaves interactively. I am pretty sure we want plt.show() to be non-blocking when run under python -i, but it would be very easy to convince me that this behavior is the correct one.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Sep 24, 2017
@jklymak
Copy link
Member

jklymak commented May 2, 2018

This hasn't been responded to for a long time so I'm closing. Feel free to reopen...

@jklymak jklymak closed this May 2, 2018
@QuLogic QuLogic removed this from the needs sorting milestone May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants