Skip to content

Conversation

@martinRenou
Copy link
Member

@martinRenou martinRenou commented Nov 2, 2018

First simple version for display of the last expression of cells (objects are first converted to string, then displayed)

@martinRenou
Copy link
Member Author

martinRenou commented Nov 2, 2018

xeus-python


builtins.attr("exec")(compiled_code, py::globals());
builtins.attr("exec")(compiled_interactive_code, py::globals());

Copy link
Member Author

@martinRenou martinRenou Nov 2, 2018

Choose a reason for hiding this comment

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

@Carreau would you mind telling me what you think about that code? What I want to do is display the last expression of the cell (e.g. "Out [3]: ...")

What I do is:

  • I use the AST to extract the last expression of the cell
  • I execute the remaining expressions normally
  • I execute the last expression in an Interactive way

Do you think it's how it should be done?

Copy link

Choose a reason for hiding this comment

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

I believe this is the same as what we do in IPython. The smaller difference is that we loop of the n-1 non-final statement manually. Not sure it makes a difference.

Copy link

Choose a reason for hiding this comment

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

Note we also have other mode where

  • all the top-level expressions trigger display
  • the last assigment also trigger display
  • Async-mode

But those can be done later or not at all I guess.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your reply! When are those modes used? Is it a command line option? (I can't find it)

Copy link

Choose a reason for hiding this comment

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

They are both CLI and dynamic options. Rarely used. So I wouldn't worry about theses yet.

m_displayhook.attr("add_hook")(publish_display);

sys.attr("displayhook") = m_displayhook;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@Carreau And here I overwrite sys.displayhook which is called when the interactive code is executed, my displayhook publish the execution result to clients.

Copy link

Choose a reason for hiding this comment

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

LGTM

If we do not do this extra check, and if the last statement is for
example:
```python
for x in range(10):
    x
```
the display hook will be triggered 10 times, while it shouldn't be
triggered at all
@SylvainCorlay SylvainCorlay merged commit 51ea8ac into jupyter-xeus:master Nov 6, 2018
@martinRenou martinRenou deleted the custom_displayhook branch November 6, 2018 12:44
SylvainCorlay added a commit to SylvainCorlay/xeus-python that referenced this pull request Mar 1, 2019
DerThorsten pushed a commit to DerThorsten/xeus-python that referenced this pull request Nov 7, 2023
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.

3 participants