有没有办法从一个%%bash或其他%%script单元格中访问当前python内核中的变量?
也许作为命令行参数或环境变量?
有没有办法在使用ipython时通过命令行将参数传递给我的python脚本?理想情况下,我想将我的脚本称为:
ipython -i script.py --argument blah
Run Code Online (Sandbox Code Playgroud)
我希望能够拥有--argument并blah列入我的sys.argv.
在 Windows 10 上,我尝试将 Jupyter 笔记本另存为 pdf,其名称会随着笔记本的每次运行而更改。
这是我到目前为止所拥有的:
name1 = 'July'
name2 = 'August'
jupyter_nb_filename = '{}_vs_{}'.format(name1,name2)
!jupyter nbconvert --output-dir="C:\\mydir\\" --output=jupyter_nb_filename --to pdf --TemplateExporter.exclude_input=True mynotebook.ipynb
Run Code Online (Sandbox Code Playgroud)
我查看了文档但无法弄清楚。
任何帮助将不胜感激。
如何从 Ipython/Jupyter notebook 执行 shell 命令,将 python 字符串变量的值作为 bash 参数中的字符串传递,如下例所示:
sp_name = 'littleGuy' #the variable
sp_details = !az ad app list --filter "DisplayName eq '$sp_name'" #the shell command
Run Code Online (Sandbox Code Playgroud)
我试过$sp_name单独使用${sp_name},{sp_name}等等,如this related question中所述,但都没有奏效。
这里的关键是变量名需要在 shell 命令中作为字符串引用。
编辑1:
@manu190466。我从字符串输出判断您的解决方案有效。出于某种原因,它在实践中似乎没有。我想知道az ad app listURL 是对查询进行编码还是什么......?
想法?
我了解有表单,但不知道如何从一个单元格获取表单输入并在其他多个单元格中使用输入。
谢谢你!
我研究过表单,但它们仅适用于非常基本的代码。我正在尝试运行一个 python 程序,这意味着我的单元代码为
!python3 <nameofprogram>.py -flags arguments
Run Code Online (Sandbox Code Playgroud)
这些参数是我想要更改的内容,但是即使使用下拉表单选择,我也无法让代码理解该参数来自表单,而不是采用文字文本,而当我更改落下。
这是单元格的代码:
Section = "tracks" #@param ["tracks", "newalbums", "justreleased", "pop", "rock", "electronic", "country", "hiphop", "rnb", "kpop", "classical", "jazz", "latin", "holiday"]
!python3 redsea.py -a TV explore atmos Section
Run Code Online (Sandbox Code Playgroud)
我看不到对我有帮助的答案。
我找到的最终答案是在代码中的片段之前使用 $ 。
!python3 redsea.py -a TV explore atmos $Section
Run Code Online (Sandbox Code Playgroud)
询问输入后。