c# - 在 aspx 页面中显示 python 的输出

标签 c# python asp.net visual-studio ironpython

一个.net新手的问题

你好,

我正在尝试在 aspx 页面中显示我的 python 代码的输出。

下面是通过单击 Button1 执行的 .net c# 代码。

protected void Button1_Click1(object sender, EventArgs e)
{
    ScriptEngine eng = Python.CreateEngine();
    dynamic scope = eng.CreateScope();
    eng.ExecuteFile(@"C:\path\hello.py", scope);
    Label1.Text = //display output in Label1
}

下面是示例 python 脚本。

print "hello"

你能告诉我如何在 Label1.Text 中显示“hello”吗?

也欢迎任何有关在 python 和 asp.net 之间传递数据的信息。

谢谢,

最佳答案

你可以把所有的结果放在变量a中

例子:

python

a = ""
a += "hello world\n"
a += "==========="

在 C# 中

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"C:\path\hello.py", scope);
var result = scope.GetVariable("a");
Console.WriteLine(result);
Console.ReadLine();

关于c# - 在 aspx 页面中显示 python 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863217/

相关文章:

c# - 我如何获取自定义属性?

c# - ServiceStack 凭据 auth 端点给出 404

javascript - 创建一个自定义用户控件,该控件使用我提供的 ID 并遵循 ClientIDMode ="Static"

html - 下拉菜单不适用于 MasterPage

c# - 是否可以仅使用用户名和密码从我的 .net core 应用程序登录 Azure?

c# - Entity Framework 代码优先 : How to ignore classes

c# - 窗口最大化时获取非最大化窗口位置/大小

python - 将 python 命名元组写入 csv 以防止 UnicodeEncodeError

python - Django 休息 : map 'update' to 'list' using CustomRouter

Python:为什么将函数名称复制到本地 namespace 会导致更快的访问