0

I am developing an app in C# WPF. The app should call Python scripts. I use IronPython for this. Unfortunately I have some difficulties. I don't get the prints from Python displayed live.

Is it possible to display the output of Print live in TextBox(like in Python IDE)?

Any help is appreciated

Daniel
  • 1
  • 1
  • Unless I missed something in your code would stop it working. I think when you do var resAdd = calculatorInstance.add(5, 14); then resAdd is a string "19". You should be able to just set the text property of a textbox to resAdd. – Andy Oct 05 '22 at 15:16
  • Does this answer your question? [How can I call (Iron)Python code from a C# app?](https://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-a-c-sharp-app) – Andy Oct 05 '22 at 15:16
  • Just split each thing you need to output into a python function and you should be able to call it from c#, get the result and set Text or whatever variable you have bound to Text. – Andy Oct 05 '22 at 15:18
  • I think you didn't quite understand my question. When I call the "add" function, I want the text from the Print command "Calculation is done." be displayed in TextBox( not only resAdd ). – Daniel Oct 05 '22 at 15:39
  • What's the problem with calling the function and setting the text then? – Andy Oct 05 '22 at 15:42
  • Calling ironpthon functions will be synchronous. When value is returned, the call to the function will be complete. – Andy Oct 05 '22 at 15:43
  • When calling the function and then setting the text, I don't know which step is currently being executed. That will only be when the function is finished – Daniel Oct 05 '22 at 15:57
  • The functions "add" and "increment" are only examples. In reality the functions are very complicated and the calculation can take hours. Via "Print" I then want to know which steps have been executed. – Daniel Oct 05 '22 at 16:00
  • I'm not sure you can output back directly to .net from ironpython without just returning. You could write to an external queue like msmq or rabbit mq. And of course, subscribe to messages in that queue from your c#. – Andy Oct 05 '22 at 18:59
  • Looks to me like that covers everything. It redirects stdout to a stream, shows how to handle getting that data from the stream. What else is there you would need? – Andy Oct 06 '22 at 12:48
  • I need the strings from the print to be displayed the moment they are set. With the current implementation they are displayed when the function/script has been executed. So I'm looking for a way to display the strings live and not after the function has been executed – Daniel Oct 07 '22 at 07:55
  • That is covered in the linked question. You output your strings to stdout in ironpython. Using code similar to what's in that question you can subscribe to an event out a stream and do whatever you want with it. Re read what's in that thread. Repeat if you don't see that it covers what you've asked for. – Andy Oct 07 '22 at 08:02
  • With the thread, the output is not displayed until the function has been executed. What I want is to display the strings live and not after the function has been executed. It should be like in a Python editor. When a function is executed, the strings are displayed sequentially while running. Back to the thread, if you have tested it, can you provide the code? – Daniel Oct 07 '22 at 08:17
  • Like I said. Running ironpython code is synchronous. You need to run the ironpython stuff on a separate thread to the ui or it'll block everything. And of course you'll need to dispatch any changes to ui properties back to the ui thread. If you need someone to write the code for you then you should understand this is not a suitable question for SO. – Andy Oct 07 '22 at 08:34
  • I don't need someone to write the code for me. That is not what I am looking for here. The idea with the separate thread for IronPython I find good. But unfortunately I don't know how to do that. Maybe you can some code lines, how it should go(As approach)? – Daniel Oct 07 '22 at 08:53

0 Answers0