# 通过插件从后端调用带参数的 Python 脚本？

**URL:** https://meta.discourse.org/t/calling-python-scripts-w-arguments-on-the-backend-via-plugin/165984
**Category:** Development
**Created:** [2020年十月2日 02:44 UTC](https://meta.discourse.org/t/calling-python-scripts-w-arguments-on-the-backend-via-plugin/165984 "2020-10-02T02:44:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![leighno5](https://avatars.discourse-cdn.com/v4/letter/l/e19b73/32.png) [@leighno5](https://meta.discourse.org/u/leighno5)
#### Post date: [2020年十月2日 02:44 UTC](https://meta.discourse.org/t/calling-python-scripts-w-arguments-on-the-backend-via-plugin/165984/1 "2020-10-02T02:44:26Z")

</div>

先把话说清楚：我完全不是前端开发人员。不过，我编写了一个 Python 脚本，它与我的 Discourse 实例运行在同一台服务器上，执行时会接收用户传入的参数。在终端中运行时，一切正如预期般正常工作。现在我需要弄清楚如何通过插件将此功能暴露给版主，让他们可以在文本框中输入所需参数，然后点击某个“执行”按钮，从而使用提供的参数运行该脚本。我完全不知道从哪里入手。如果有人能给我指个方向，那就太好了。这是否只需从[编写插件指南](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515)开始即可？我确实看过这份指南，但当时并没有觉得有什么内容直接符合我的需求。如果我只是需要再仔细研读一遍，而且那确实是最佳的起点，请告诉我！谢谢！

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2020年十月2日 07:54 UTC](https://meta.discourse.org/t/calling-python-scripts-w-arguments-on-the-backend-via-plugin/165984/2 "2020-10-02T07:54:29Z")

</div>

您可以使用 [Running external commands with Open3 - makandra dev](https://makandracards.com/makandra/44452-running-external-commands-with-open3) 在 Ruby 和插件中调用命令行。

我在我的 Discourse Frotz（互动小说）插件中使用了它：[discourse-frotz/lib/frotzbot.rb at 6054d38af0ab8e516a93416026f8e646f5c5ea56 · merefield/discourse-frotz · GitHub](https://github.com/merefield/discourse-frotz/blob/6054d38af0ab8e516a93416026f8e646f5c5ea56/lib/frotzbot.rb#L167%EF%BC%8C%E4%BB%A5%E4%BE%BF%E4%B8%8E%E7%BB%88%E7%AB%AF%E4%BA%A4%E4%BA%92%E3%80%82)

但如果您打算走这么远，或许可以考虑完全使用原生 Ruby 编写。它是一门非常令人愉悦的语言。

---

<div class="post-metadata">

### Author: ![leighno5](https://avatars.discourse-cdn.com/v4/letter/l/e19b73/32.png) [@leighno5](https://meta.discourse.org/u/leighno5)
#### Post date: [2020年十月2日 16:29 UTC](https://meta.discourse.org/t/calling-python-scripts-w-arguments-on-the-backend-via-plugin/165984/3 "2020-10-02T16:29:04Z")

</div>

非常感谢，真的很感谢您的帮助！
