Friday, 13 May 2011

PyPad: Exploring .NET with IronPython

So I have been doing lots of extending with Visual Studio that I’m still about to write about, and wanted an easier way to explore the environmnet.

Enter PyPad I got this idea (bear in mind) you can do this with IronPython console. But something that with a few lines of code I can enter an IDE and explore the objects, inspect them and also the .NET environment.

Consider the following .NET Winform App:

   1: public Form1()
   2:        {
   3:            InitializeComponent();
   4:            Text = "PyPad";
   5:            _pypad = new UcPyPad();
   6:            _pypad.SetVariable("HostForm", this);
   7:            _pypad.Dock = DockStyle.Fill;
   8:            Controls.Add(_pypad);
   9:            Width = 800;
  10:            Height = 600;
  11:            _pypad.RunScript("");
  12:        }

 I am hosting my PyPad control on this form, so it will look like this:

image


So we want to change the caption?

image

Use either the Script Editor

Or the interactive window

Notice each time you run the script you can assign new objects and see them in the Globals list as well as any members they may have.


This way you can “explore” the environment and fix problems bit by bit without restarting the application. I will be looking at more and more creative ways to use PyPad for future projects.
How its built

See my earlier post on Hosting IronPython scripts to get the ScriptController. Then for a beautiful script editor see Schintilla.NET and some controls and Library I put together.

Then

   1: public void RunInteractiveCode(string Code)
   2: {
   3:     LogMessage(">> " + Code + System.Environment.NewLine);
   4:     RunScript(Code);
   5: }
   6:  
   7: public void RunScript(string Code)
   8: {
   9:     try
  10:     {
  11:        
  12:         Compile(Code);
  13:         var retval = _context.Execute();
  14:         if (retval != null)
  15:         {
  16:             retval = _context.CreateLocalScope()
  17:                 .SetVariable("expressionToEvaluate", retval)
  18:                 .Evaluate("repr(expressionToEvaluate)");
  19:           
  20:             LogMessage(retval.ToString() + System.Environment.NewLine);
  21:         }
  22:         UpdateVariables();
  23:  
  24:     }
  25:     catch (Exception e)
  26:     {
  27:         LogMessage(e.Message + System.Environment.NewLine);
  28:     }
  29: }

Get the code
Hosting Project:

https://github.com/TheWalkingDev/Projects/tree/master/DevTools/PyPad/PyPad

Component Library:

https://github.com/TheWalkingDev/ACSR

No comments:

Post a Comment