access host class from IronPython script (lightswitch)
Question: I'm new at this, so I hope the question is well formed enough for someone to
understand what i'm asking, if not I'm happy to add more detail. I am trying
to reference a variable defined on the server side of a lightswitch
application from a python script. The following post explains how to access a
host class from a python script.

[Access host class from IronPython
script](http://stackoverflow.com/questions/6234355/access-host-class-from-
ironpython-script)

The code on my server side is

    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Security.Server;
    using System.IO.Ports;
    using System.Threading;
    using IronPython.Hosting;
    
    namespace LightSwitchApplication
    {
     public partial class ApplicationDataService     
    {
        partial void ServerCommands_Inserting(ServerCommand entity)
        { 
            switch (entity.ClientCommand)
            {
                case "RunPythonScript":
    
    
                 var engine = Python.CreateEngine();
                 var searchPaths = engine.GetSearchPaths();
                 searchPaths.Add(@"C:\Temp");
                 engine.SetSearchPaths(searchPaths);
    
                 var mainfile = @"C:\Temp\script.py";
                 var scope = engine.CreateScope();
                 engine.CreateScriptSourceFromFile(mainfile).Execute(scope);
                 var param1 = entity.Param1;                       
                 engine.Execute(scope);
    

How do i reference the server side variable below from the Python script?

    
    
    entity.Param1
    

In the python script I've attempted to import a server side class that would
allow me access to the variable

    
    
    import clr
    clr.AddReference("Microsoft.Lightswitch.Application")
    

but the .Server reference is not available...only the .Base is available.

    
    
    from Microsoft.Lightswitch.Framework.Base import
    

I have no idea if the Framework.Server class is even the right one, just
guessing at this point. Thanks!

[![enter image description
here](http://i.stack.imgur.com/iZgS7.png)](http://i.stack.imgur.com/iZgS7.png)


Answer: You could simply set `entity` as a variable of your scope. You also could pass
it to a function in `script.py`. For example:

Your python script:

    
    
    class SomeClass:
        def do(self, entity):
            print entity.Param1
    

Your c# code will look like this:

    
    
    var engine = Python.CreateEngine();
    var searchPaths = engine.GetSearchPaths();
    searchPaths.Add(@"C:\Temp");
    engine.SetSearchPaths(searchPaths);
    
    var mainfile = @"C:\Temp\script.py";
    var scope = engine.CreateScope();
    
    // Execute your code
    engine.CreateScriptSourceFromFile(mainfile).Execute(scope);
    // Create a class instance
    var type = scope.GetVariable("SomeClass");
    var instance = engine.Operations.CreateInstance(type);
    // Call `do` method
    engine.Operations.InvokeMember(instance, "do", entity); // PASS YOUR ENTITY HERE
    

After this, you can access your complete entity in the IronPython script. You
don't need `clr.AddReference("Microsoft.Lightswitch.Application")`.

Also, the second execute (`engine.Execute(scope);`) seems to be not
neccessary. Maybe this helps you.

