Skip to content
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

Sharing success w/ shell integration for a jupyter console and ipython workflow #4475

Closed
randomgeek78 opened this issue Jan 8, 2022 · 4 comments

Comments

@randomgeek78
Copy link

randomgeek78 commented Jan 8, 2022

Update: providing patches for ipython as well.

I recently upgraded to 0.24 and explored the shell integration features. I was delighted with these features and thought it would be great within my nvim + jupyter console workflow. My current workflow requires a lot of cut+paste from jupyter console to a text file. I was able to hack jupyter console so it works well with the shell integration features. Now, when I run a command, I no longer have to select and then paste. I just get the entire output selected using shell integration and then paste it where I wanted. This required me to insert the escape codes at the right locations in the jupyter_console/ptshell.py file. Everything works like a charm now. I am the relevant patches below for others to try-

  if [[ -e "$site_packages"/jupyter_console/ptshell.py ]]; then
    echo "Patching $site_packages/jupyter_console/ptshell.py"
    perl -0777 -pi -e "$(cat <<'_E_'
s{\Q
        if PTK3:
            text = await self.pt_cli.prompt_async(default=default)
\E}
{
        def sys_write\(\):
            import sys
            sys.stdout.write\(u"\\u001b\\u005d133;A\\u001b\\u005c"\)
        pre_run = sys_write

        if PTK3:
            text = await self.pt_cli.prompt_async(default=default, pre_run=pre_run)
}gsx
_E_
)" "$site_packages"/jupyter_console/ptshell.py

    perl -0777 -pi -e "$(cat <<'_E_'
s{\Q
        while self.client.iopub_channel.msg_ready():
            sub_msg = self.client.iopub_channel.get_msg()
            msg_type = sub_msg['header']['msg_type']
\E}
{
        while self.client.iopub_channel.msg_ready():
            sub_msg = self.client.iopub_channel.get_msg()
            msg_type = sub_msg['header']['msg_type']

            if msg_type in ['execute_input', 'execute_result']:
                sys.stdout.write(u"\\u001b\\u005d133;C\\u001b\\u005c")
}gsx
_E_
)" "$site_packages"/jupyter_console/ptshell.py
  fi

  if [[ -e "$site_packages"/IPython/terminal/interactiveshell.py ]]; then
    echo "Patching $site_packages/IPython/terminal/interactiveshell.py"
    perl -0777 -pi -e "$(cat <<'_E_'
s{\Q
        if old_loop is not self.pt_loop:
            policy.set_event_loop(self.pt_loop)
        try:
            with patch_stdout(raw=True):
                text = self.pt_app.prompt(
                    default=default,
                    **self._extra_prompt_options())
\E}
{
        if old_loop is not self.pt_loop:
            policy.set_event_loop(self.pt_loop)
        try:
            sys.stdout.write\(u"\\u001b\\u005d133;A\\u001b\\u005c"\)
            with patch_stdout(raw=True):
                text = self.pt_app.prompt(
                    default=default,
                    **self._extra_prompt_options())
}gsx
_E_
)" "$site_packages"/IPython/terminal/interactiveshell.py
  fi

  if [[ -e "$site_packages"/IPython/core/interactiveshell.py ]]; then
    echo "Patching $site_packages/IPython/core/interactiveshell.py"
    perl -0777 -pi -e "$(cat <<'_E_'
s{\Q
        try:
            result = self._run_cell(
                raw_cell, store_history, silent, shell_futures)
\E}
{
        try:
            sys.stdout.write\(u"\\u001b\\u005d133;C\\u001b\\u005c"\)
            result = self._run_cell(
                raw_cell, store_history, silent, shell_futures)
}gsx
_E_
)" "$site_packages"/IPython/core/interactiveshell.py
  fi
# kitty.conf
mouse_map shift+right press ungrabbed paste_from_selection
mouse_map right press ungrabbed mouse_select_command_output
map kitty_mod+s  paste_from_selection
@kovidgoyal
Copy link
Owner

Cool, I have added a link to this issue in the shell integration docs
under third party integrations.

@randomgeek78
Copy link
Author

Thank you.

@randomgeek78 randomgeek78 changed the title Sharing success w/ shell integration for a jupyter console workflow Sharing success w/ shell integration for a jupyter console and ipython workflow Jan 17, 2022
@eo1989
Copy link

eo1989 commented May 28, 2022

Where do we copy pasta the above code into?

@ces42
Copy link
Contributor

ces42 commented Sep 25, 2022

I think the idea is you set the site_packages variable and run the code in a shell - it will modify ipython's source files (probably not a great thing but it works). The code looks for certain spots in the ipython source files, but it might be an old version of ipython -- on my system I had to search for the spots in interactiveshell.py that look similar to those in randomgeek78's patch script and manually add sys.stdout.write(u"\u001b\u005d133;A\u001b\u005c") there.

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

No branches or pull requests

4 participants