0

I am using Visual C# as UI and Python in the background.

  1. Enter the details on a visual C# form.
  2. Clicking a button should run a Python program which should embed the details given in the form into an XML file.
  3. Python should process the XML and ingest into a system.
  4. Python should monitor for the success from logs and return back the value to be displayed in C# form.

Is this possible?

5
  • Have you tried anything? Commented Mar 10, 2015 at 11:24
  • "I am using Visual C# as UI and Python in the background" - why? Why not write the back-end in C#, or use IronPython for .NET interoperability? Commented Mar 10, 2015 at 11:27
  • Jon... I got to know from many colleagues to use c# as back - end. So i might re-think on that Commented Mar 10, 2015 at 12:41
  • Jon... How do i use IronPython? Which one do you suggest? i wanted a nice UI to serve my Python scripts Commented Mar 10, 2015 at 15:15
  • i wanted a nice UI to serve my Python scripts try kivi Commented Mar 10, 2015 at 22:00

1 Answer 1

2

You could start your python in a new process in the background and pass the form items as arguments in the process start information as if you were running the python script from the command line, like so:

var start = new ProcessStartInfo
            {
                FileName = _pathToPythonExecutable,
                Arguments = string.Format(" {0} --arg1 {1}",_pathToYourPythonScript, //formItemValue),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                WorkingDirectory = _currentWorkingDirectory            
            };
using (Process process = Process.Start(start))
            { 
                // Do stuff
            }

You'll see that in my start information, I have told the process to Redirect StandardInput, Standard Output and Standard Error. This is another way that you can pass data between the processes.

You would write to standard input like so:

process.StandardInput.Write(input);
process.StandardInput.Close();

Standard output and Standard Errors are streams, so you can read them like so

// This would be the same for standard error
using (StreamReader reader = process.StandardOutput)
                {
                    result = reader.ReadToEnd();
                }
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks a lot David. I will try it out and will update
@Roshanr Yeah, make sure you let me know. I would suggest that you use a C# back end, but if you cannot avoid using python, like it was in my situation, I believe this should handle it for you
jon... I agree with you to use c# as back end. Since i wanted to use Python only but with some nice UI, i cant avoid using it. I have just tested the same you gave and it worked. Thanks a lot
@Roshanr fantastic, great to hear. Glad I could help!
Sorry that i mentioned Jon instead of David :)
|

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.