Skip to content

set_option as a context manager #5618

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
jseabold opened this issue Nov 29, 2013 · 11 comments · Fixed by #5752
Closed

set_option as a context manager #5618

jseabold opened this issue Nov 29, 2013 · 11 comments · Fixed by #5752
Labels
Milestone

Comments

@jseabold
Copy link
Contributor

It doesn't look like it's available yet as such, but it might be nice to have set_option usable as a context manager. E.g., I often find myself wanting to do something like

with pd.set_option('max_rows', 200):
    print df

As simple as adding __enter__ and __exit__ methods and preserving state?

@jreback
Copy link
Contributor

jreback commented Nov 29, 2013

you have to do something like:

with pd.option_context('max_rows',200):
      print df

IIRC

@jseabold
Copy link
Contributor Author

Oh nice. Is it new? I don't see it in the namespace. Probably deserves a mention in the set_option docstring too.

@jreback
Copy link
Contributor

jreback commented Nov 29, 2013

I think it's been around
mostly for internal use
let's make this issue a doc issue then

@jseabold
Copy link
Contributor Author

config_prefix according to #2097. And it's not exposed at the top-level namespace AFAICT.

@ghost
Copy link

ghost commented Nov 30, 2013

option_context: #2516, and it was extended later to accept multiple key/value pairs, I think at cpcloud's suggestion.

so you can

with option_context(foo,"is1", bar, "is2"):
    baz

@jseabold
Copy link
Contributor Author

Ah, great. Then consider this a request for importing it into the top-level namespace and adding some docs.

@ghost
Copy link

ghost commented Nov 30, 2013

I have no objections to that. Can't remember the details but I think the reason set_option
itself isn't a context_manager was that it clashed with the nifty dynamic docstring
magic attached to it, which no one ever noticed or used. Should probably just ditch that
and make set_option a context manager instead. More intuitive.
Can just calculate the docstring once at the end of config_init and set it instead
of all that fuffing around in config.py.

@ghost
Copy link

ghost commented Dec 31, 2013

Thanks @jseabold.

@oztalha
Copy link

oztalha commented Feb 23, 2017

Running this in a Jupyter notebook shell does not print anythin:

with pd.option_context('display.max_colwidth', -1):
    HTML(guesses.to_html(escape=False))

but this does work as expected:

pd.set_option('display.max_colwidth', -1)
HTML(guesses.to_html(escape=False))

I expected to get the same result but I did not. Any ideas, why? Is it a problem on the Jupyter side or re the implementation of the context? Thanks.

@TomAugspurger
Copy link
Contributor

@oztalha wrap your HTML(...) in an IPython.display.display. The notebook won't treat the version inside the context manager as a "return value" to be displayed.

@oztalha
Copy link

oztalha commented Feb 23, 2017

Thanks, worked just as you said:

with pd.option_context('display.max_colwidth', -1):
    display(HTML(guesses.to_html(escape=False)))

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants