Steven Nagy on March 05, 2009 14:33 sez:
Interesting... (the IronPython integration that is, not this article). Now we can embed Quake2 style console popdowns in all our applications.
Steven Nagy on March 05, 2009 14:33 sez:
Interesting... (the IronPython integration that is, not this article). Now we can embed Quake2 style console popdowns in all our applications.
tarn on March 05, 2009 18:36 sez:
That's fantastic, but the fun has only just begun! If you change SourceCodeKind.SingleStatement to .Statements or .File you should be able to run this IronPython code in your app..
-- CUT --
import clr
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReference('IronPython')
clr.AddReference('Microsoft.Scripting')
from System.Windows.Forms import *
from IronPython.Hosting import Python
from Microsoft.Scripting import SourceCodeKind
class MetaNote(Form):
def __init__(self):
self.button = Button()
self.code = TextBox()
self.text = TextBox()
self.Width = 400
self.code.Multiline = True
self.code.Height = 100
self.code.Width = 300;
self.text.Top = 100
self.text.Width = 400
self.text.Height = 300
self.text.Multiline = True
self.button.Left = 300
self.button.Text = "Go"
self.Controls.Add(self.button)
self.Controls.Add(self.text)
self.Controls.Add(self.code)
self.button.Click += self.run
def run(self,sender,e):
engine = Python.CreateEngine()
source = engine.CreateScriptSourceFromString(self.code.Text, SourceCodeKind.Statements)
scope = engine.CreateScope()
scope.SetVariable("txt", self.text);
source.Execute(scope)
f = MetaNote()
f.Show()
-- CUT --
again and again ;)
Bengt on March 06, 2009 02:48 sez:
Use ironscheme and implement emaclisp! :)
Mr Graviton Tepes on March 06, 2009 07:19 sez:
I really like Python!
> Use ironscheme and implement emaclisp! :)
Would it be possible to write emacs in Python?
OJ on March 07, 2009 01:29 sez:
> Use ironscheme and implement emaclisp! :)
*sigh* There's always one isn't there.
Great post LB. I had no idea that embedding Iron(P/R) would be so easy! Great demo.
Tarn, interesting addition, though arguably geeking out a bit ;)
ev on March 08, 2009 08:05 sez:
you know, i always wonder why would you do this (i.e. embed any scripting language in .NET Framework app), if you already have c#/vb compiler available.
Here is very nice project by Oleg Shilo which, AFAIK, grew from similar line of thinking: http://www.csscript.net/
PS. sorry in advance for resubmitting comment, but the first time i submitted it from Opera i got no notice about premoderation or anything else keeping my comment from showing up.
lb on March 08, 2009 15:35 sez:
@ev
>why would you do this if you already c#/vb compiler available ?
Good question!
Basically, c#/vb aren't always the right tool for the job.
I've embedded c#/vb into an application before and it was much more complex to perform, and the final result was much less flexible to the task.
Here's three examples of why a dynamic language is a better choice for embedding.
1. You don't need to worry about an entry point.
In C#/VB a single statement can't exist by itself -- you need to wrap them inside a class definition, and in order to make them executable, you need to setup an explicit entry point (for example in a console app, you have 'main').
This is a detail that you can hide from the end user, but it's still there and likely to lead to more complexity.
2. In C#/VB, the minimum unit is an entire assembly. With a dynamic language, you can get an Abstract Syntax Tree.
If you need to do something with the compiled code other than run it, then having an abstract syntax tree can be useful. With a full assembly, you can use reflection and CodeDom to inspect more detail, but this is considerably more involved.
3. Iron python is more succinct.
I can also think of arguments *against* using IronPython -- for example, if the end user has no familiarity with it, or willingness to gain any.
If 'raw performance' is required then maybe a dynamic language will hold you back (but then again, maybe a plugin solution is not the best approach in such a case anyhow)
2xThomas on April 10, 2010 19:32 sez:
Mr Graviton Tepes, sure. You can write emacs in Python if you want to. Don't see why you would want to do that.
Open up the Python interpreter console and then type: emacs. Congratulations! You have now written emacs in Python. Wow!
Bob on July 09, 2017 01:18 sez:
That was much simpler than expected. Nice.
Juha Kuusama on July 24, 2017 09:05 sez:
Year 2017, things have changed somewhat. Using Visual Studio 2017 and IronPython 2.7.7:
Install IronPython from Nuget package manager console, two lines:
Install-Package IronPython
Install-Package IronPython.StdLib
Most of the references are installed automatically for you. Then, everything you want to use from IP has to be public on C# side. Here is how to access a variable, a function and a component (texbox, made public on the designer):
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
engine = IronPython.Hosting.Python.CreateEngine();
scope = engine.CreateScope();
scope.SetVariable("Main", this);
}
static public ScriptEngine engine;
static public ScriptScope scope;
public int a = 0;
public void incrA()
{
a = a + 2;
}
private void Script_button_Click(object sender, System.EventArgs e)
{
try
{
string code =
"Main.a= Main.a + 5\n" +
"Main.incrA()\n" +
"Main.textBox1.Text= 'message a='+str(Main.a)";
ScriptSource source = engine.CreateScriptSourceFromString(code,
SourceCodeKind.AutoDetect);
source.Execute(scope);
}
catch (System.Exception ex)
{
string txt = ex.Message; // show the message somehow...
}
}
}
}
Andrew on November 27, 2017 18:08 sez:
Very nice tutorial. I used to embed Tcl/Tk back in the days of old and happily scripting has come a long, long way since.