P Raju P Raju - 2 months ago 6
ASP.NET (C#) Question

Display output from python in aspx page

Question from a .net newbie

Hello,

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.

Thanks,

Answer

You can put all your result in variable a

Example:

Python

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");
Console.WriteLine(result);
Console.ReadLine();