我想将一个 IPython 笔记本与一些 shell 命令及其输入放在一起。在 bash 提示符下,我可以使用“here-document”语法:
bash-3.2$ mysql -u root <<END_IPUT
> use mydb;
> show tables;
> END_INPUT
如何在 IPython 中,特别是在 jupyter 笔记本中获得相同的效果?我知道如何将 shell 命令作为 IPython 执行为“line magics”或“cell magics”,例如:
In [7]: !! ls -tF
Out[7]: ['Demo-notebook.ipynb',
'createdb.sql',
...
我将IPython 视为一个系统外壳,它展示了如何启用一些语法细节。在以下之后,我可以运行系统命令而无需预先!
或!!
# Turn everything in $PATH into an alias;
# then enable calling aliases without ! or %
%rehashx
%autocall 2
但是这些都无助于为这些命令提供内联输入:here-document 语法在 IPython 中无效,并导致 python SyntaxError
. 那么我该怎么做呢?