2016-10-04 27 views
2

問題,顯示輸出在aspx頁面從.NET新手</p> <p>你好

我試圖從aspx頁面我的Python代碼顯示輸出。

下面是.net c#代碼,通過點擊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 
} 

下面是示例python腳本。

print "hello" 

請問您能告訴我如何在Label1.Text中顯示「hello」嗎?

還有任何有關在python和asp.net之間傳遞數據的信息是讚賞。

感謝,

+0

那麼你的'工程.ExecuteFile() '回來? – Juggernaut

回答

3

你可以把你所有的結果變量

例子:

的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(); 
+0

非常感謝。這像寶石一樣工作。 –

+0

ScriptEngine不支持。我應該下載什麼DLL? –

+0

此外,如果我有1k行的Python代碼,那麼我必須只將輸出放在變量中,而不是在變量的1k行代碼中。如果錯誤請糾正我。 – AskMe