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

Add new interactivity mode for ast. #10598

Merged
merged 1 commit into from Jun 1, 2017

Conversation

Projects
None yet
4 participants
@Carreau
Member

Carreau commented May 24, 2017

Allow to display the result of the last assignment of it has a unique
target.

meh, I should write tests for that.

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 24, 2017

Member

@rgbkrk though on that ? I though about that because display() is long to type and I often do

df = some_operation(df)
df 

Just to display it.

Member

Carreau commented May 24, 2017

@rgbkrk though on that ? I though about that because display() is long to type and I often do

df = some_operation(df)
df 

Just to display it.

@rgbkrk

This comment has been minimized.

Show comment
Hide comment
@rgbkrk

rgbkrk May 24, 2017

Member

If this behavior exists, out of old Matlab habit I will append ; in hopes that it will not show the result by default.

I too do the operation then display it pretty often. I'd hate to have to figure out how to suppress output though.

Member

rgbkrk commented May 24, 2017

If this behavior exists, out of old Matlab habit I will append ; in hopes that it will not show the result by default.

I too do the operation then display it pretty often. I'd hate to have to figure out how to suppress output though.

@rgbkrk

This comment has been minimized.

Show comment
Hide comment
@rgbkrk

rgbkrk May 24, 2017

Member

Oh I see, this isn't the default setting, just a configurable. 🆒

Member

rgbkrk commented May 24, 2017

Oh I see, this isn't the default setting, just a configurable. 🆒

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 24, 2017

Member

yeah, of course it's not the default :-)

$ ipython --InteractiveShell.ast_node_interactivity='last_expr_or_assign'
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: a = 1;

In [2]: a = 1
Out[2]: 1

In [3]:

; works, you can also do a newline / pass or newline / None

Member

Carreau commented May 24, 2017

yeah, of course it's not the default :-)

$ ipython --InteractiveShell.ast_node_interactivity='last_expr_or_assign'
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: a = 1;

In [2]: a = 1
Out[2]: 1

In [3]:

; works, you can also do a newline / pass or newline / None

@Carreau Carreau requested review from ivanov and takluyver May 24, 2017

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 24, 2017

Member

codetransformer 0.7.0 has been released

Thanks ! Though I'm on 3.6, so can't import it :-(

Pushed new commits that test the various behavior.

Member

Carreau commented May 24, 2017

codetransformer 0.7.0 has been released

Thanks ! Though I'm on 3.6, so can't import it :-(

Pushed new commits that test the various behavior.

@Carreau Carreau modified the milestone: 6.2 May 29, 2017

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 29, 2017

Member

@fperez would prefer for this to not assign to Out[] and _.
I'm unsure about that. So I can rewrite the Ast to actually do display(ast_name).
I'll see what I can do.

Member

Carreau commented May 29, 2017

@fperez would prefer for this to not assign to Out[] and _.
I'm unsure about that. So I can rewrite the Ast to actually do display(ast_name).
I'll see what I can do.

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 29, 2017

Member

Hum inject something at the end like that:

In [10]: %%ast
    ...: __import__('IPython').display.display(whatever)
Module(
  body=[
    Expr(
      value=Call(
        func=Attribute(
          value=Attribute(
            value=Call(
              func=Name(id='__import__', ctx=Load()),
              args=[
                Str(
                  s='IPython',
                ),
              ],
              keywords=[],
            ),
            attr='display',
            ctx=Load(),
          ),
          attr='display',
          ctx=Load(),
        ),
        args=[
          Name(id='whatever', ctx=Load()),
        ],
        keywords=[],
      ),
    ),
  ],
)

At the same time when the expression of a of a cell display something there is always an Out[], so I"m unsure.

Member

Carreau commented May 29, 2017

Hum inject something at the end like that:

In [10]: %%ast
    ...: __import__('IPython').display.display(whatever)
Module(
  body=[
    Expr(
      value=Call(
        func=Attribute(
          value=Attribute(
            value=Call(
              func=Name(id='__import__', ctx=Load()),
              args=[
                Str(
                  s='IPython',
                ),
              ],
              keywords=[],
            ),
            attr='display',
            ctx=Load(),
          ),
          attr='display',
          ctx=Load(),
        ),
        args=[
          Name(id='whatever', ctx=Load()),
        ],
        keywords=[],
      ),
    ),
  ],
)

At the same time when the expression of a of a cell display something there is always an Out[], so I"m unsure.

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver May 30, 2017

Member

If we're injecting display() into the builtins, that could be simpler. Although then I guess it would be susceptible to someone overriding display =.

Maybe there's a nicer way to do this - instead of modifying the AST, inspect it to find the target of the final assignment, and then record some information which says 'if this code completes successfully, print the value of foo'.

Member

takluyver commented May 30, 2017

If we're injecting display() into the builtins, that could be simpler. Although then I guess it would be susceptible to someone overriding display =.

Maybe there's a nicer way to do this - instead of modifying the AST, inspect it to find the target of the final assignment, and then record some information which says 'if this code completes successfully, print the value of foo'.

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 30, 2017

Member

Maybe there's a nicer way to do this - instead of modifying the AST, inspect it to find the target of the final assignment, and then record some information which says 'if this code completes successfully, print the value of foo'

Well that's almost exactly what the above do. it calls display on the last assignment target.

Here is my argument against having it displayed and not triggerd as part of display hook.

One the the two below methods are doing

def load_something():
    display(...)

The other

def load_somethingelse():
    return ...
$ ipython
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: %config TerminalInteractiveShell.ast_node_interactivity='display_last_expr_or_assign'
In [2]: from dummymod import load_cars, load_iris

In [3]: a = load_iris()

=================
 Id    |  types
=================
1      |    A
2      |    B
3      |    A
None

In [4]: b = load_cars()

=================
 Id    |  types
=================
I      |    E
II     |    F
III    |    G

With the current implementation that would be disambiguated by having (or not) an Out:

In [6]: a = load_iris()

=================
 Id    |  types
=================
1      |    A
2      |    B
3      |    A

In [7]: b = load_cars()
Out[7]:

=================
 Id    |  types
=================
I      |    E
II     |    F
III    |    G

Well now both are implemented so I can push 2 options.

Member

Carreau commented May 30, 2017

Maybe there's a nicer way to do this - instead of modifying the AST, inspect it to find the target of the final assignment, and then record some information which says 'if this code completes successfully, print the value of foo'

Well that's almost exactly what the above do. it calls display on the last assignment target.

Here is my argument against having it displayed and not triggerd as part of display hook.

One the the two below methods are doing

def load_something():
    display(...)

The other

def load_somethingelse():
    return ...
$ ipython
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 13:19:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: %config TerminalInteractiveShell.ast_node_interactivity='display_last_expr_or_assign'
In [2]: from dummymod import load_cars, load_iris

In [3]: a = load_iris()

=================
 Id    |  types
=================
1      |    A
2      |    B
3      |    A
None

In [4]: b = load_cars()

=================
 Id    |  types
=================
I      |    E
II     |    F
III    |    G

With the current implementation that would be disambiguated by having (or not) an Out:

In [6]: a = load_iris()

=================
 Id    |  types
=================
1      |    A
2      |    B
3      |    A

In [7]: b = load_cars()
Out[7]:

=================
 Id    |  types
=================
I      |    E
II     |    F
III    |    G

Well now both are implemented so I can push 2 options.

@rgbkrk

This comment has been minimized.

Show comment
Hide comment
@rgbkrk

rgbkrk May 30, 2017

Member

Ok I'm totally going to set this in my personal defaults.

Member

rgbkrk commented May 30, 2017

Ok I'm totally going to set this in my personal defaults.

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 30, 2017

Member

Note, the current code will also not work if the LHS is an arbitrary assignment like a.b = 1

Member

Carreau commented May 30, 2017

Note, the current code will also not work if the LHS is an arbitrary assignment like a.b = 1

@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau May 30, 2017

Member

So @fperez agree with the original proposal. I'm going to revert ca387c8

Member

Carreau commented May 30, 2017

So @fperez agree with the original proposal. I'm going to revert ca387c8

Add new interactivity mode for ast.
Allow to display the result of the last assignment of it has a unique
target.
@Carreau

This comment has been minimized.

Show comment
Hide comment
@Carreau

Carreau Jun 1, 2017

Member

Now that 6.1 is released, I'm getting this in to play with it.

Member

Carreau commented Jun 1, 2017

Now that 6.1 is released, I'm getting this in to play with it.

@Carreau Carreau merged commit beb08ba into ipython:master Jun 1, 2017

4 checks passed

codecov/patch 97.91% of diff hit (target 0%)
Details
codecov/project 66.89% (+0.06%) compared to be46bc7
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@Carreau Carreau deleted the Carreau:ast-interactivity branch Jul 20, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment