在 aspx 页面中显示 python 的输出

Display output from python in aspx page

.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();