Hi Oleksii & Sharon,

If you've done this before, would you share which I can get a console window in embedded Python.Net and get the Python string output from C#?

Thank you,
Spark.


On Tue, Mar 1, 2011 at 1:33 PM, Oleksii Bidiuk <oleksii.bidiuk@gmail.com> wrote:
Hi Sharon,

I haven't found a way to run a console, but I had achieved a similar effect by redirecting the stdout and stderr from within Python (as suggested some time ago by Guido himself in a related embedded CPython post) to a .NET object and then show the incoming strings in a TextBlock (I am using a WPF app that hosts Python.NET). Below is a very short description of what I have done. Please let me know if this is indeed what you were looking for (or perhaps somebody else), I will prepare a more detailed post then (need to recap details from code).

To achieve having 'live' console output I have used some 'setup script' and 'clean up' scripts executed before and after the user-defined script. In the setup script I first backup the curent value of sys.std* objects and then assign my own (.NET) object to sys.stdout and in the clean up part I restore the original std* values (similar to the sample for inserting certain path to the sys.path discussed before).

To make use of the technique described above you need to have a .NET object that implements the same interface as python stream (meaning write, writelines, etc methods, see dir(sys.stdout) for all the details). In my experience write() and writelines() is enough. Having that you can pass an instance of such object to your Python code. As soon as you have the information coming through your .NET object methods you can do things like raising events, adding text to GUI, dump to a file, etc. It does require a bit of additional work, (which I honestly already expected to see already in Python.NET) and to some extend similar to the concept of console redirection per script context in IronPython, although in IronPython it is done in a 'native' .NET way.

I am not sure this is THE best solution and other suggestions are more than welcome!


2011/3/1 Sharon Rozenblum <Sharon.Rozenblum@sandisk.com>

Hi!

 

Is there a way to show the console window when running Python from C# (in order to see the Python outputs, when the c# project is not console application)

 

Thanks,

 

SHARON

 

 


--
oleksii

_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
http://mail.python.org/mailman/listinfo/pythondotnet