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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Roshan r
  • 522
  • 2
  • 11
  • 30

1 Answers1

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();
                }
David Watts
  • 2,249
  • 22
  • 33
  • Thanks a lot David. I will try it out and will update – Roshan r Mar 10 '15 at 12:57
  • @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 – David Watts Mar 10 '15 at 14:24
  • 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 – Roshan r Mar 10 '15 at 15:13
  • @Roshanr fantastic, great to hear. Glad I could help! – David Watts Mar 10 '15 at 15:14
  • Sorry that i mentioned Jon instead of David :) – Roshan r Mar 10 '15 at 15:18
  • @Roshanr No problem. Kind of used to it. I have a brother close to my age that people sometimes confuse my name for. If this was satisfactory, please accept it so it is visible to others who it may help. – David Watts Mar 10 '15 at 15:19
  • How do i accept this? Since i am new to stackoverflow, i am not sure how to do that.. But if you can give me one more tip that will be great. How can i get the processed value from Python back to my C# form? – Roshan r Mar 10 '15 at 15:32
  • @Roshanr There should be a Tick underneath the Upvote/Downvote buttons to the left of my answer. You use those buttons to show appreciation for questions and answers if you found them helpful. If you asked the question, Like you did this one, then you use the tick to say that this answers your question. – David Watts Mar 10 '15 at 15:34
  • I did now. Can you please give me a solution for the last one i asked – Roshan r Mar 10 '15 at 15:36
  • There is plenty you can do within your Python itself to monitor a log file and then feed the result into the Standard Output for your C# code to read. I'll see if I can find some links for you, but that is a seperate question. It is best if each question you ask is a clear question on its own. This answer gets you the ability to satisfy the first three points I guess. The 4th is python specific. Consider asking another question. – David Watts Mar 10 '15 at 15:39
  • @Roshanr http:[This Question](http://stackoverflow.com/questions/3290292/read-from-a-log-file-as-its-being-written-using-python) should start to point you in the right direction about reading the log file. But again, probably better asking that one as a different question. – David Watts Mar 10 '15 at 15:41
  • Still i am stuck here. Can you please guide me? My Visual C# code is Process process = new Process(); process.StartInfo.FileName = @"C:\Python27\python.exe"; process.StartInfo.Arguments = string.Format("{0} {1}",@"C:\Work\Scripts\XML_Parsing.py", "asset_id.Text"); process.StartInfo.WorkingDirectory = @"C:\c#\Work\RulesValidator\RulesValidator"; process.StartInfo.RedirectStandardError=true; process.StartInfo.UseShellExecute = true; process.StartInfo.RedirectStandardOutput = true; I dont see python being called – Roshan r Apr 27 '15 at 14:24
  • @Roshanr what are you stuck with? Also, from the code in that comment, you have not started the process, just set it up. You should be able to call process.Start() and it should do what it needs to. – David Watts Apr 27 '15 at 14:26
  • I started the process. Due to the limitation on the number of lines to post, i couldn't post it here. I am in the middle of changing few lines of code. Once it's done, i should be able to give you a clear picture – Roshan r Apr 28 '15 at 14:43