1

I have a simple python program named hello.py

import sys
def Simple():
    print "Hello from Python"

How do I display the output of this program in a text box located in a Web Form of type .aspx.cs

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

    TextBox1.Text = // Output of hello.py to be asigned to TextBox1.Text
}
0

1 Answer 1

1

You have to follow these steps to achieve this.

Host that python file in CGI, lets say link is http://localhost/cgi-bin/test.py and invoke that using WebClient and get output. Following are code for invoking URL using WebClient.

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    WebClient client = new WebClient();
    string reply = client.DownloadString("http://localhost/cgi-bin/test.py"); // address = CGI hosted URL
    TextBox1.Text =  reply;
}

ELSE

Assumption: You have python installed in windows system.

  1. Save python script to a file , say abc.py.
  2. You can directly execute python script, using this "c:\python26\python.exe" "abc.py". We will use this in next step.
  3. Using process execution, we can execute the above command from C# and get output. Example-
var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "c:\python26\python.exe",
        Arguments = "abc.py",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do something with line
    TextBox1.Text  = line;
}

OR - use this link http://blog.luisrei.com/articles/flaskrest.html - this shows , how to make python code REST based thing, and you can invoke directly that REST API in asp.net code.

Sign up to request clarification or add additional context in comments.

Comments

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.