P Raju P Raju - 10 months ago 43
ASP.NET (C#) Question

Display output from python in aspx page

Question from a .net newbie


I am trying to display output from my python code in aspx page.

Below is .net c# code that executes by the click of Button1.

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

Below is the sample python script.

print "hello"

Could you please tell me how to display "hello" in Label1.Text?

Also any kind of information regarding passing data between python and asp.net is appreciated.


Answer Source

You can put all your result in variable a



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

In C#

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