在IPython中使用curses。我怎样才能改善这个? [英] Working with curses in IPython. How can I improve this?

查看:249
本文介绍了在IPython中使用curses。我怎样才能改善这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了一种与诅咒交互式工作的方法,同时仍然享受着IPython的大部分好处。它的作用有一定的局限性,但并不像我想的那么好。



原来的问题当然是我希望能够工作使用curses(ncurses)模块(或 urwid )使用我的交互式Python会话,同时控制终端屏幕, 例如)。一种解决方案是编写一个简单的TCP服务器,其中包含一个简单的事件循环,用于计算从套接字读取的每个字符串,并发回代表并返回结果的序列化字符串。如下所述: SO:有办法吗?交互式编程Python curses应用程序)。



这是一个稍微简单的技巧(假设您安装了IPython)。

 #!/ usr / bin / python 
#!/ usr / bin / env python
来自IPython import embed_kernel
import curses

def interact_with_curses(屏幕):
'''设置全局stdscr变量并运行嵌入式IPython内核
适合由curses.wrapper()调用
'' '
global stdscr
stdscr = screen
embed_kernel()

if __name__ =='__ main__':
curses.wrapper(interact_with_curses)

(稍微有点想让SO的语法高亮一点)。



运行此命令将导致输出粗糙喜欢:

  [IPKernelApp]要将另一个客户端连接到此内核,请使用:
[IPKernelApp] - 存在kernel-2869.json

切换到另一个窗口或屏幕会话,你可以运行:

  ipython console  - 现有kernel-2869.json 

连接到该流程并使用它。



这已经足够了。然后你可以调用 stdscr.refresh()之类的东西。使用你的curses / window和pad对象,在它们上面调用 dir()来探索它们的功能,并且通常使用代码,就像你在正常的IPython会话中一样碰巧正在更​​新不同终端的屏幕并从中读取(通过curses输入功能)。



这种方法的问题和问题:




  • 要退出,似乎我必须从IPython控制台运行quit(),并且这不会以正常方式退出解释器。它似乎不允许 curses.wrapper()重置终端和各种尝试调用 .endwin() .resetty()(当然,在执行了 .savetty()之后), .reset_shell_mode()(和 .reset_prog_mode())依此类推都失败了。在调用 curses.wrapper()之后我尝试在main中调用它们,我尝试用 atexit


    • 如何从这样的会话中彻底退出?


  • [Tab]完成无效


    • 如何让IPython的[Tab]完全通过这些嵌入式内核之一的IPython控制台会话?


  • 调用IPython embed_kernel() 函数将套接字信息输出到curses屏幕,该屏幕已经被 curses.wrapper()初始化。这很丑陋;如果想要做更多有趣的工作,在curses中,在调用 embed_kernel()函数之前,我无法看到由该函数打印到stdout或stderr的文本。


    • 如何使 embed_kernel()无声并强制注册连接详细信息通过其他一些机制?我可以给它自己的套接字名称/路径使用吗?




<我确信我会想到其他问题,但我希望其他人会发现这个技巧很有用,并且当我想涉及Python curses编码时,我会发现其他一些技巧。

解决方案

事实证明,我们现在可以以相当自然的方式使用IPython来交互式地处理诅咒。



从一个终端输入:

  ipython kernel 

这将打印一行如下:

  [IPKernelApp] To将另一个客户端连接到此内核,使用:
[IPKernelApp] - 现有kernel-14321.json

从另一个终端/窗口类型:

  ipython console  - 现有kernel-14321.json 

...你将处于看似完全正常的IPython会话中。唯一的区别是你实际上是在另一个窗口中访问远程IPython内核会话。从那里你可以使用curses函数,查看其他窗口中的更改,输入其中的输入,使用[Tab] -completion等等。



注意[Ctrl] - [D]将提供退出IPython 控制台(客户端),而 quit()将关闭IPython 内核(远程窗口---服务器)。



但总的来说,这个模型比我去年在我的问题中描述的更清晰,更容易。我不知道它是不是更新版本的IPython(0.13.1),还是简单的无知让我以前的尝试有点笨拙。


I've found a way to interactively work with curses while still enjoying most of the benefits of IPython. It works, with some limitations, but not as well as I'd like.

The original problem, of course, is that I'd like to be able to work with my interactive Python session while having it control a terminal screen, using the curses (ncurses) module (or urwid, for example). One solution is to write a simple TCP server with a simple event loop that evaluates each string it reads from the socket and send back serialized strings representing and return results. As described here: SO: Is there a way to interactively program a Python curses Application).

Here's a somewhat simpler trick (assuming you have IPython installed).

    #!/usr/bin/python 
    #!/usr/bin/env python 
    from IPython import embed_kernel
    import curses

    def interact_with_curses(screen):
        '''set global stdscr variable and run embedded IPython kernel
           suitable to be called by curses.wrapper()
        '''
        global stdscr
        stdscr = screen
        embed_kernel()

    if __name__ == '__main__':
        curses.wrapper(interact_with_curses)

(slightly munged to get SO's syntax highlighting happy).

Running this will result in output roughly like:

 [IPKernelApp] To connect another client to this kernel, use:
            [IPKernelApp] --existing kernel-2869.json

And switching to another window or screen session you can run:

ipython console --existing kernel-2869.json

To connect to that process and work with it.

This is nice enough. You can then calling things like stdscr.refresh(). Work with your curses/window and pad objects, call dir() on them to explore their functionality and generally work with the code as if you were in a normal IPython session which just happens to be updating a different terminal's screen and reading from it as well (through the curses input functions).

Problems with this approach, and questions:

  • To exit it seems that I have to run quit() from the IPython console, and this doesn't exit the interpreter in the normal means. It doesn't seem to allow curses.wrapper() to reset the terminal and various attempts to called .endwin(), .resetty() (after having performed a .savetty() of course), .reset_shell_mode() (and .reset_prog_mode()) and so on have all failed. I've tried calling them in main after the call to curses.wrapper() and I've tried registering them with atexit
    • How do I cleanly quit from such a session?
  • [Tab] completion doesn't work
    • How do I get IPython's [Tab] completely working through an IPython console session to one of these embeded kernels?
  • Calling the IPython embed_kernel() function prints the socket information to the curses screen, which is already initialized by the curses.wrapper() by that time. This is ugly; also if want to do more interesting work, in curses and before calling the embed_kernel() function then I can't see the text which was printed to stdout or stderr by that function.
    • How do I make embed_kernel() silent and force it to register the connection details through some other mechanism? Can I give it my own socket name/path to use?

I'm sure I'll think of other questions, but I hope others will find this trick useful and will discover some other tricks I can use when I want to dabble with Python curses coding.

解决方案

It turns out that we can now just use IPython in a fairly natural manner for interactively working with curses.

From one terminal simply type:

ipython kernel

This will print a line something like:

[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-14321.json

From another terminal/window type:

ipython console --existing kernel-14321.json

... and you'll be in a seemingly perfectly normal IPython session. The only difference will be that you're actually accessing the "remote" IPython kernel session in the other window. From there you'll be able to use curses functions, see changes in the other window, type inputs thereto, use [Tab]-completion, and so on.

Note that [Ctrl]-[D] will offer to exit the IPython console (client) while quit() will close the IPython kernel (remote window --- server).

But, overall this model is cleaner and easier then what I described in my question last year. I don't know if it's the newer version of IPython (0.13.1) or if was simple ignorance that made my previous attempts somewhat clunkier.

这篇关于在IPython中使用curses。我怎样才能改善这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆