When an ipython console connect to an existing kernel linked to a notebook, %load magic can not load code into console - ipython

I learned this trick from Using IPython console along side IPython notebook . But when I connect this kernel using console, the %load magic will load file into a pager (like you you do 'man thecodefile.py' in console), not to the input line.
Anyone know how to change these behavior back to default?

You ask two question here, please ask them separately.
As for !su ausername there is no way to forward stdin through subprocess.

Related

How to create a jupyter-lab extension that interacts with a running kernel

I am trying to create an extension for jupyter-lab that interacts with a running ipython kernel, running either in a console or a notebook.
As a minimal example I am trying to get two "hello world" type programs. The first should print a message to stdout of the running kernel that is input in the extension. The second should log a message to the console of the extension that is input in the running ipython kernel.
To understand jupyter-lab extension I started with the astronomy picture of the day! tutorial. Although this covers the basics of extensions this does not address interacting with a running kernel.
From "really confused with jupyter notebook lab extensions and ipywidgets"! it seems I should be looking at the Comms module however this is documentation for Jupyter-notebook and not for Jupyter-lab. As far as I understand, Jupyter-lab is sufficiently different under the hood that this should not work.
Could someone explain to me how to create an extension that interacts with a running kernel and what the appropriate documentation/tutorial is to look at if it exists?

Configure Jupyter Lab to log kernel interactions

I am trying to figure out a way to capture logs rendered through users interaction with the kernel in Jupyter Lab.
After you jupyter lab --generate-config there will be configuration file with a few logging-related options available:
c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'
c.Application.log_format = '[%(name)s]%(highlevel)s %(message)s'
c.Application.log_level = 30
With these, it seems to generate some verbose output in the terminal but can someone point me in direction to help understand the following:
How can I get this to log into a file?
This configuration seems to be generating logging handlers which could be altered in the notebook code easily - is there a way to get a lower end logger that can not be altered in user code?
Is this a right way to capture kernel logs (I am looking for something that would capture all the inputs and maybe even outputs sent kernel way)?

Determine if we're in an IPython notebook session

I want my function to do one thing if it's called from the IPython notebook and another thing if its called from a console or library code. In particular I'm making a progress bar with the desired following behavior:
Notebook: Immediately return a widget
Console: Block and dump information to sys.stdout
Is there a flag somewhere I can check to determine if the user called my function from a notebook or otherwise?
You can't detect that the frontend is a notebook with perfect precision, because an IPython Kernel can have one or more different Jupyter frontends with different capabilities (terminal console, qtconsole, notebook, etc.). You can, however, identify that it is a Kernel and not plain terminal IPython:
import sys
def is_kernel():
if 'IPython' not in sys.modules:
# IPython hasn't been imported, definitely not
return False
from IPython import get_ipython
# check for `kernel` attribute on the IPython instance
return getattr(get_ipython(), 'kernel', None) is not None
Since the notebook is so much more popular than other frontends, this indicates that the frontend is probably a notebook, but not conclusively. So you want to be sure there's a way out for the more primitive frontends.

Where is Ipython's kernal.js located on a Linux Ubuntu system?

Seems like a silly question, but I cannot find the kernal.js script and I think I need to edit it for my installation...(see reason below if interested, though it's not part of the question). Does anyone know where it would be located on a Linux Ubuntu system? I've tried find and grep, but not getting anywhere.
I am serving ipython notebook with a reverse proxy in front, and I am finding that the path to the kernals is wrong, causing errors when I try to load a notebook. I think this line is a problem:
var ws_url = this.ws_url + this.kernel_url;
Because it excludes the port number suffix from ws_url, which is needed as I am not using the default port with my reverse proxy setup.
I realise it would be an ugly hack to change it, but it's only for development purposes.
Check /usr/share/ipython/notebook/static/services/kernels/js/kernel.js

How to use make-frame-on-display and multi-tty in Emacs?

This page from the Emacs manual describes a function called make-frame-on-display that allows you to access a remote Emacs session. My interest in this function is to use it to share buffers for pair programming remotely with a colleague.
From the page:
It is even possible to use this feature to let two or more users type simultaneously on the two displays, within the same Emacs job. In practice, however, the different users can easily interfere with each others' edits if they are not careful.
How exactly do I set this up? What do I need? What does my partner need? The details are not stated, but I don't know enough about Emacs to know where to start.
Is there any other way to get a shared Emacs session? This page from the Emacs Wiki refers to something called multi-tty. The questions I asked above also apply to this.
Which is better: multi-tty or make-frame-on-display?
I haven't tried multi-tty, but make-frame-on-display is pretty simple. You type M-x make-frame-on-display, hit return, then type the display you want the frame to show up on. For example:
I have my local host (thor) running emacs and I want to make a frame pop up on a machine called zeus, on its only X display (0.0). So I would type M-x make-frame-on-display<ret>zeus:0.0<ret>
All set!
You may need to configure the remote machine's X server to accept incoming connections from your machine with "xhost +thor". You may also need to configure its firewall to allow incoming connections on the X11 port, which is 6000. Keep in mind that X forwarding is not encrypted, so if you aren't working with someone on your LAN you may want to go through a VPN in order to keep things private.
Edited to fix brackets.
You can also have the person at zeus type ssh -X thor emacsclient -c.
Did you ever consider using GNU screen on a shared account for pair programming? It's dead easy to get it to work and you get to pick any console based editor you and your partner like (emacs, vim, joe, nano, zile, ...). However, this does of course not work with editors that cannot run inside a terminal.
To set it up, create a shared account on a computer running ssh. Then both log in to that account. One of the partners starts screen with
screen
and the other connects to it with
screen -x
where -x means "attach to a not detached screen session". The users can detach from their sessions w/ "C-a d".
Old question, new solution for anyone landed on this page from year 2016.
I set this up in Ubuntu 14.04 and it works perfectly:
Suppose I want to co-edit or demo some cool stuffs on emacs with my colleague Joe on the other end of the world.
Make sure an ssh server with emacs installed at either end. That is, either MyPC or JoePC must be a SSH server and have Emacs installed. From now on, let's say I asked Joe to install SSH server and Emacs on his computer.
Make sure byobu is installed on JoePC. Byobu supports both tmux and screen as backends (I prefer tmux for a more mordern and feature-rich).
I connect to JoePC with ssh remoteuser#joepc. No need for X-forwarding.
Open emacs from byobu-ssh terminal: TERM=xterm-256color && emacsclient --alternate-editor="" -t. One can make an alias for this command. I recommend this long command because it enables both better color support in the terminal and running Emacs in daemon mode. The daemon mode make it fast to close and reopen frames.
Now the magic unfold: Ask Joe to login with the same remoteuser I am loggin in; open the terminal and start byobu if it hasn't for him.
Start hacking or fumbling :-)
Comment:
Because the way byobu work. This approach works for any other program inside the byobu terminal.
This setup performs much better than Teamviewer or any other GUI remote desktop solutions.
Because the connection is through ssh to the remote server directly, it is as secure as the ssh conenction can offer.