3

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,

1
  • what does your eng.ExecuteFile() return? Commented Oct 4, 2016 at 23:54

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

3 Comments

ScriptEngine is not supporting. what should i download any dll?
Moreover, if I have 1k lines of Python code then then I have to put only the output in the variable NOT the 1k lines of code in variable....right?? please correct me if wrong.
@SunnySandeep Just install the nuget package IronPython 2.2.7 and add the directives: using IronPython.Hosting; using Microsoft.Scripting.Hosting;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.