How to embed a graphical interactive IronPython shell in an application?
Asked Answered
G

2

10

I've tried the obvious path in my pet open source project RevitPythonShell (a plugin for the building modeling software Autodesk Revit Architecture 2010): code.interact() with the IronPython engine set up to use .NET streams for STDIN and STDOUT. These I then redirect to a TextBox control. It kinda works, but really is only an ugly hack.

The main problem is getting all the shell stuff to work: Up-arrow and down-arrow for history, editing, copy & paste, EOF, syntax highlighting, tool tips etc. It takes a lot of work to get this right, and it is not really the problem I'm trying to solve (I'm trying to get an interactive shell hosted in Revit, not make the perfect shell GUI).

If this weren't a .NET project, I'd probably look into reusing PyCrust/PyWrap, but I'm not sure if that can be done from a WinForms project. Is there anything similar for .NET?

Has anyone ever implemented the IConsole interface and can show an example of what I'd need to do? It seems this would be the proper route to go (as opposed to using the code module), but for the life of me I can't figure it out. IronPython source code has no comments whatsoever.

UPDATE: After trying out some stuff, I eventually settled on the superb IronLab code. It includes an example shell with syntax highlighting and all. The code was nice and easy to integrate. Check the RevitPythonShell code on hints on how to embed it.

Gusella answered 4/3, 2010 at 9:33 Comment(2)
Not really a nice canned solution...so I'll leave it as a comment. You could look at Matt Ward's PythonConsole.cs source in the SharpDevelop IDE project. Look in SharpDevelop\src\AddIns\BackendBindings\Python\PythonBinding\Project\Src. It might be a big problem to break this class out so that it (and the ICSharpCode.TextEditor) can be used in other projects. But perhaps it will help you out a bit. Download the SharpDevelop source from their build server: build.sharpdevelop.net/BuildArtefacts or use svn at: svn://svnmirror.sharpdevelop.net/sharpdevelop/branches/3.0/SharpDevelopImaginable
don't be shy ;) this is a very good answer in its own right - I will have a look into it as soon as I get around to doing so!Gusella
D
7

I was looking for a similar thing and I discovered that the IronLab project contains a nice IronPython console. The source code can be found here https://github.com/rwg0/ironlab/tree/master/IronPythonConsole.

[edit on 25/10/2016] fixed broken url

Devilmaycare answered 21/3, 2012 at 19:59 Comment(2)
right, i actually moved to this about a year ago, but forgot to update my question. thanks for documenting it for the rest of the world. the IronPythonConsole from the IronLab project is a gem! You can see an example of embedding it in your own project in the RevitPythonShell project.Gusella
This link is dead, unfortunately.Archambault
I
3

Ok, then I have two possible answers. One in the comment above....and another far simpler (no intellisense, no syntax highlighting):

http://www.codeproject.com/KB/edit/irontextbox2.aspx

It's by JoeSox, and it is for IronPython 2, not 2.6. However I was able to update it to work properly with 2.6 in about five minutes. If you are interested I can figure out a way to send you a diff file or slap the updated project somewhere (it was published under an MIT license).

Enjoy!

David

Imaginable answered 4/3, 2010 at 20:0 Comment(2)
sweet :) just what I was looking for! Will report back here when I get it running!Gusella
you can look at the source of the RevitPythonShell (revitpythonshell.googlecode.com) for an idea of how to integrate this the IronTextBox2 control...Gusella

© 2022 - 2024 — McMap. All rights reserved.