__file__ = './00_core.ipynb'
pshnb IPython magic
Provides
psh
persistent bash magics in Jupyter and IPython
= dict(os.environ, TERM='dumb', PS1='$', PS2='$')
env = os.environ['SHELL']
eshell = pexpect.spawn(eshell, encoding='utf-8', env=env) sh
= dict(os.environ, TERM='dumb', PS1='', PS2='')
env = os.environ['SHELL']
eshell = pexpect.spawn(eshell, encoding='utf-8', env=env)
sh 'stty -echo')
sh.sendline(
sh.readline()= os.urandom(8).hex()
echo = re.compile(fr'^{echo}\s*$', flags=re.MULTILINE)
echo_re f'export PS1=""')
sh.sendline('set +o vi +o emacs')
sh.sendline('echo '+echo)
sh.sendline(=2) sh.expect(echo_re, timeout
0
'ls | head -3')
sh.sendline('echo '+echo)
sh.sendline(=2)
sh.expect(echo_re, timeoutprint(sh.before)
00_core.ipynb
CHANGELOG.bak
CHANGELOG.md
ShellInterpreter
ShellInterpreter (debug=False, timeout=2, shell_path=None, sudo=False, dumb=False)
Initialize self. See help(type(self)) for accurate signature.
= ShellInterpreter()
sh print(sh('ls | head -3'))
00_core.ipynb
CHANGELOG.bak
CHANGELOG.md
= ShellInterpreter(sudo=True)
sh
'cd')
sh(print(sh('pwd'))
'cd ..')
sh(print(sh('pwd'))
print(sh('whoami'))
/var/root
/var
root
shell_replace
shell_replace (s, shell=None)
Replace @{var}
refs in s
with their variable values, if they exist
= 1
b
= '''asdf
a $@{b} @{aa}
fdsa'''
print(shell_replace(a))
asdf
$1 @{aa}
fdsa
PshMagic
PshMagic (shell, sudo=False, timeout=2, expand=True, o=None)
Initialize self. See help(type(self)) for accurate signature.
create_magic
create_magic (shell=None)
# Only required if you don't load the extension
create_magic()
%psh pwd
/Users/jhoward/Documents/GitHub/pshnb
%psh cd ..
%psh pwd
/Users/jhoward/Documents/GitHub
%%psh
> tmp << EOF
cat
hi
there EOF
%psh cat tmp
hi
there
%psh rm tmp
%psh ls | head -3
ContextKit
FastHTML-Gallery
aimagic
= 2 n
%psh echo @{n}
2
%psh ls | head -@{n}
ContextKit
FastHTML-Gallery
%%psh
echo starting1; echo finished) & (sleep
starting
[1] 99418
%psh
finished
[1]+ Done ( sleep 1; echo finished )
%psh -h
::
%psh [-h] [-r] [-o] [-x] [-X] [-s] [-S] [-t TIMEOUT] [command ...]
Run line or cell in persistent shell
positional arguments:
command The command to run
options:
-h, --help Show this help
-r, --reset Reset the shell interpreter
-o, --obj Return this magic object
-x, --expand Enable variable expansion
-X, --no-expand Disable variable expansion
-s, --sudo Enable sudo
-S, --no-sudo Disable sudo
-t TIMEOUT, --timeout TIMEOUT
Set timeout in seconds
%psh pwd
/Users/jhoward/Documents/GitHub
%psh -r
%psh pwd
/Users/jhoward/Documents/GitHub/pshnb
%psh -s
%psh whoami
root
%psh -S
%psh whoami
jhoward
%psh -t 1
try: get_ipython().run_line_magic('psh', 'sleep 2')
except TIMEOUT: print("timed out")
timed out
load_ipython_extension
load_ipython_extension (ipython)
Required function for creating magic
create_ipython_config
create_ipython_config ()
Called by pshnb_install
to install magic