如何在 VS Code 中运行 Streamlit 多命令,无论默认终端如何

发布于 2025-01-11 14:09:50 字数 1383 浏览 0 评论 0原文

我在 VS Code 上安装了 多命令 扩展并使用它使用通过键盘快捷键运行的 settings.json 中的此配置来启动 Streamlit 应用程序:

"multiCommand.commands": [
    {
        "command": "multiCommand.streamlitActiveFile",
        "label": "Streamlit: Run Active File",
        "description": "Streamlit run active file in active terminal",
        "sequence": [
            "workbench.action.terminal.focus",
            {
                "command": "workbench.action.terminal.sendSequence",
                "args": {
                    "text": "streamlit run ${relativeFile}\u000D"
                }
            }
        ]
    }
]

到目前为止,当我在 Windows 计算机上使用标准命令提示符时,这一切运行良好。现在我已更改为使用 Git Bash 作为默认终端,上面的命令失败,因为相对文件路径作为带有反斜杠的 Windows 路径传递,而 Git Bash 无法解析它:

$ streamlit run visuals\streamlit\time_plot.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]...
Try 'streamlit run --help' for help.

Error: Invalid value: File does not exist: visualsstreamlittime_plot.py

简而言之,反斜杠被忽略并且路径变成单个文件名。我想要某种方式能够通过键盘快捷键使用多命令启动 Streamlit 应用程序,并且我愿意接受这些解决方案中的任何一个,或者其他我没有考虑过的解决方案:

  • 发送一个相对文件路径,该路径可以无论哪个终端是默认终端,都能正确解析。
  • 即使 Git Bash 是默认终端,也启动 Windows 命令提示符,并在那里激活 Streamlit 应用程序。
  • 将工作目录更改为包含 Streamlit 文件的目录(同样,无论哪个终端是默认终端)并在那里启动该文件,而无需给出路径。

I have the Multi-command extension installed on VS Code and use it to launch Streamlit apps using this configuration in settings.json which is run via a keyboard shortcut:

"multiCommand.commands": [
    {
        "command": "multiCommand.streamlitActiveFile",
        "label": "Streamlit: Run Active File",
        "description": "Streamlit run active file in active terminal",
        "sequence": [
            "workbench.action.terminal.focus",
            {
                "command": "workbench.action.terminal.sendSequence",
                "args": {
                    "text": "streamlit run ${relativeFile}\u000D"
                }
            }
        ]
    }
]

This has worked fine thus far while I have been using the standard command prompt on a Windows machine. Now I've changed to using Git Bash as my default terminal, and the command above fails because the relative file path is passed as a Windows path with backslashes, and Git Bash can't parse that:

$ streamlit run visuals\streamlit\time_plot.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]...
Try 'streamlit run --help' for help.

Error: Invalid value: File does not exist: visualsstreamlittime_plot.py

In short, the backslashes are ignored and the path becomes a single file name instead. I'd like some way to be able to launch Streamlit apps using a multi-command via a keyboard shortcut, and I'm open to either of these solutions, or something else I haven't considered:

  • Send a relative file path that can be parsed correctly regardless of which terminal is the default terminal.
  • Launch a Windows command prompt even when Git Bash is the default terminal, and activate the Streamlit app there.
  • Change the working directory to the directory containing the streamlit file (again regardless of which terminal is the default terminal) and launch the file there without having to give a path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文