简体   繁体   English

搜索名称/关键字“将 python 脚本功能添加到应用程序”

[英]Searching for name/keywords for, "adding python scripting capabilities to an app"

I'm building an app that I want the user to be able to add functionality to by writing their own Python script.我正在构建一个应用程序,我希望用户能够通过编写自己的 Python 脚本来添加功能。

The problem is, I'm not 100% sure how to go about it, or what such a feature would be called...问题是,我不是 100% 确定如何 go 关于它,或者这样的功能会被称为什么......

So, I was wondering if anybody knew what the feature was called or knew of some keywords that may help me find what I'm looking for.所以,我想知道是否有人知道该功能的名称或知道一些可以帮助我找到所需内容的关键字。


So... what exactly is it that I want to do?那么……我到底想要做什么? Aside from the main features of the app, I've got a textbox that I want the user to be able to write Python scripts into.除了应用程序的主要功能之外,我还有一个文本框,我希望用户能够将 Python 脚本写入其中。 Then, when the user is done writing their script, they click one of two buttons below the text box.然后,当用户完成脚本编写后,他们单击文本框下方的两个按钮之一。 One to save the script and the other to execute the script.一个用于保存脚本,另一个用于执行脚本。 To be clear, I'm not trying to build an IDE.需要明确的是,我并不是要构建 IDE。 I'm trying to build an app with the ability to allow the user to expand functionality by adding new algorithms.我正在尝试构建一个能够允许用户通过添加新算法来扩展功能的应用程序。

Now, the idea I came up with for making this all possible was for the "execute" button to save everything as a.py file and then run it over the terminal.现在,我想出的让这一切成为可能的想法是让“执行”按钮将所有内容保存为 .py 文件,然后在终端上运行它。 But that seems a bit too... "sloppy" in my mind.但这似乎有点太……在我看来“草率”。 It seems to me there ought to be a better more "elegant" solution.在我看来,应该有一个更好更“优雅”的解决方案。 And that's what I'm here asking...这就是我在这里要问的...

Is there a more "elegant" solution that keeps everything within the app(doesn't require the app to pass the user's script through Terminal)?是否有更“优雅”的解决方案将所有内容保留在应用程序中(不需要应用程序通过终端传递用户的脚本)? If so what is it called?如果有,它叫什么? What are some keywords I could search for to help me better understand it?我可以搜索哪些关键字来帮助我更好地理解它?


For w/e it's worth... I'm building an image processing app that has a bunch of features;对于 w/e 它是值得的……我正在构建一个具有许多功能的图像处理应用程序; but, I can't think of every possible feature.但是,我想不出所有可能的功能。 Soooo.... why not let the users add their own algorithms to the app's library? Soooo .... 为什么不让用户将自己的算法添加到应用程序的库中?


I'm writing the app in Python3 on a Mac.我正在 Mac 上用 Python3 编写应用程序。

Python has a built-in function called exec which lets you run any string containing Python code. Python 有一个内置的 function 称为exec ,它允许您运行任何包含 Python 代码的字符串。

s = """
for i in range(5):
    print(i)
"""
exec(s) # prints 0 1 2 3 4

It also lets you pass a dictionary similar to what globals() would give you to initialize a namespace and also observe how the namespace comes out the other side, which is useful if you need to retrieve a value from your user's code.它还允许您传递一个类似于globals()的字典来初始化命名空间,并观察命名空间如何从另一端出来,这在您需要从用户代码中检索值时很有用。

namespace = dict()
s = """
my_var = 420
hello = "world!"
"""
exec(s, dict(), namespace)
# at this point namespace == {'my_var': 420, 'hello': 'world!'}

But you should be very careful if you plan to use this.但是如果你打算使用它,你应该非常小心。 Letting users run any Python code allows for exactly the kind of consequences you'd expect.让用户运行任何 Python 代码可以实现您所期望的那种结果。 If it's just a hobby project, I'd say go ahead, knock yourself out.如果这只是一个爱好项目,我会说 go 前面,把自己搞砸了。 I've used this plenty in the past myself, where I am absolutely sure only I will be using it.我自己过去使用过很多,我绝对确定只有我会使用它。 But if you plan to use this on software running on anyone's computer, I'd say: don't.但是,如果您打算在任何人的计算机上运行的软件上使用它,我会说:不要。 You don't know what kind of code people are going to be writing, but they're going to blame you once someone inevitably writes malicious code using your program.你不知道人们会写什么样的代码,但是一旦有人不可避免地使用你的程序编写了恶意代码,他们就会责怪你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM