Using Sudo Inside Jupyter Notebook's Cell

using sudo inside jupyter notebook's cell

Update: I checked all the methods, all of them are working.


1:

Request password using getpass module which essentially hides input by user and then run sudo command in python.

 import getpass
import os

password = getpass.getpass()
command = "sudo -S apt-get update" #can be any command but don't forget -S as it enables input from stdin
os.system('echo %s | %s' % (password, command))

2:

 import getpass
import os

password = getpass.getpass()
command = "sudo -S apt-get update" # can be any command but don't forget -S as it enables input from stdin
os.popen(command, 'w').write(password+'\n') # newline char is important otherwise prompt will wait for you to manually perform newline

NOTE for above methods:

The field where you enter the password may not appear in the ipython
notebook. It appears in the terminal window on a mac, and I imagine it
will appear in a command shell on a PC. Even the result details would appear in the terminal.

3:

You can store your password in mypasswordfile file and just type in cell :

!sudo -S apt-get install blah < /pathto/mypasswordfile # again -S is important here

I would prefer this method if I want to view output of the command in jupyter notebook itself.

References:

  1. Requesting password in IPython notebook

  2. https://docs.python.org/3.1/library/getpass.html

  3. Using sudo with Python script

sudo ipython - how to launch ipython with sudo power

To run sudo command in ipython, I found out I do not necessary need to run ipython with "sudo". Instead, I run ipython and use the magic word "!" to run linux command with sudo access:

%ipython
%!sudo lspci
%!sudo ...some command

Using sudo with Python script

sudoPassword = 'mypass'
command = 'mount -t vboxsf myfolder /home/myuser/myfolder'
p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

Try this and let me know if it works. :-)

And this one:

os.popen("sudo -S %s"%(command), 'w').write('mypass')

Subscript variables with numbers in Jupyter notebook

Answer after question edit

There are two important things to know about Jupyter: There's a server, which basically runs the actual web page, file handling, and stuff like that; and then there's the kernel, which does the actual computation. So, for example, everything you actually type into a cell should be in the language of the kernel.

The server will always be some version of python, but the kernel can be practically anything at this point — python, julia, R, or any from a long list of others. When you evaluate a cell, behind the scenes it's python taking that code, sending it to the kernel, and then writing the output back to the web page. But normally, you shouldn't need to think about this at all.

Tab completion ties into the kernel, meaning that the language used for the kernel determines what can be tab completed. From the screenshot in the edited question, we can see that the kernel is actually python, which doesn't allow subscripted numerals.

To switch to the julia kernel, go up to the notebook's toolbar and select "Kernel" > "Change kernel" > "Julia [something]". If Julia isn't one of the options, you need to install it with IJulia, and probably refresh the notebook's web page.

It's also possible for Jupyter to get confused about which kernel it's on. This is a bug that I've come across when switching kernels. Try starting a new notebook with the Julia kernel via "File" > "New Notebook" > "Julia [something]".

It takes a little longer for the Julia kernel to start up than a python kernel, but once it says "Kernel ready", you should be able to get tab completion.


Older answer

a\_1<TAB> works for me. I do find Jupyter buggy, so sometimes I have to wait a little for TAB to do anything. Also, if there's anything following the text I'm trying to tab-complete, it frequently doesn't work at all. But it's definitely possible.

In fact, all the numbers should work, along with ()+-=, quite a few lowercase Latin letters, a few lowercase Greek letters, and schwa. Not all subscripted characters work, of course, because they don't exist in Unicode, for some reason.

How to run jupyter notebook in the background ? No need to keep one terminal for it

You can put the process into the background by using jupyter notebook --no-browser & disown. You can close the terminal afterwards and the process will still be running.

If you're using zsh you can also use a shorter version that does the same: jupyter notebook --no-browser &!.

To kill the process you can use pgrep jupyter to find the PID of the process and then kill 1234, replacing 1234 with the PID you just found.

Explanation

The --no-browser flag makes jupyter not open the browser automatically, it also works without this flag.

The & puts it into the background of the currently running shell.

The disown then removes the job from the background of the currently running shell and makes it run independently of the shell so that you may close it.

In the zsh version the &! is a built-in function that does the same as & disown.

iruby no bash commands possible in jupyter notebook

To execute commands at a system level you can use any of the following: Kernel#system, %x (see below for more about percent strings), or Kernel#` ("back ticks")

system('pwd')
%x(pwd)
`pwd`

These will all run in process and return the output as a String.

If you want the commands to run in a separate process you can use other options such as IO.popen or fork { exec('pwd') } ( if supported by the OS)

TL;DR Why the error is occurring

Now to explain your actual error and why it is occurring.

This line is but %%bash is being interpreted by ruby and it is looking for a closing %.
% is another way of creating a string literal (called a "Percent String").

The interesting part about this syntax is it does not really have a defined delimiter. (essentially most non-alphanumerics can open and close)

%{bash}
#=> "bash"
%(bash)
#=> "bash"
%|bash|
#=> "bash"

The last line of the docs states

If you are using “(”, “[”, “{”, “<” you must close it with “)”, “]”, “}”, “>” respectively. You may use most other non-alphanumeric characters for percent string delimiters such as “%”, “|”, “^”, etc.

In your case % is being used as the delimiter: e.g.

%%bash%
#=> "bash"

but since there is no closing % this is being interpreted as:

%%bash
pwd
--EOF--

Thus the error you are receiving because the String ("bash\npwd) is unterminated.



Related Topics



Leave a reply



Submit