📜 ⬆️ ⬇️

Ruby | Python in the browser, do it yourself

What for?

sidebar_gestalt Client part in web applications can be created in JavaScript. I believe that most developers with great pleasure would have moved away from this rule and used their favorite server-side language. Well, the MIX Online team provided fans of dynamic languages ​​with such an opportunity, for more information on this, see “ Gestalt Project - write in Ruby, Python and XAML directly in HTML on the client side ”. Below, I propose to create something similar, but on my own.

With using what?

After seeing a few examples, it became clear to me that Gestalt is implemented on Silverlight + Dynamic Languages ​​Runtime. Next, I figured, but how difficult is it to create something with your own hands? And the solution right away: Silverlight is able to interact with the DOM - it means I can get the code and interpret it - things are easy - implementation.

Let's get started

We will need:

Lead code

image Open Visual Studio, create the Silverlight Application, I’ll call the project “mygestalt”. Now I realize that I will not have to write much at all, I will need an experimental Client-Script and its interpreter. Open the page on which our Silverlight will be hosted, in my case it is mygestaltTestPage.aspx and add our client-side python code there. It should look something like this:
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
  1. <script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
  2. <script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
  3. <script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
  4. <script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
  5. <script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
Next, go to MainPage.xaml.cs, where we will be looking for our script:
  1. using System.Linq;
  2. using System.Windows.Browser;
  3. namespace mygestalt
  4. {
  5. public partial class MainPage
  6. {
  7. public MainPage ()
  8. {
  9. Initializecomponent ();
  10. FindAndRunScript ();
  11. }
  12. private void FindAndRunScript ()
  13. {
  14. var scripts = HtmlPage.Document.GetElementsByTagName ( "script" );
  15. var pythonScript = scripts.Where (x => x.GetProperty ( "type" ) .ToString () == "python" ) .First ();
  16. PythonEngine.Run (pythonScript.GetProperty ( "innerHtml" ) .ToString ());
  17. }
  18. }
  19. }
* This source code was highlighted with Source Code Highlighter .
Well, the implementation of PythonEngine:
  1. using Microsoft.Scripting;
  2. using Microsoft.Scripting.Hosting;
  3. using Microsoft.Scripting.Silverlight;
  4. namespace mygestalt
  5. {
  6. public static class PythonEngine
  7. {
  8. public static ScriptScope Run ( string source)
  9. {
  10. var setup = Configuration.LoadFromAssemblies (Package.GetManifestAssemblies ());
  11. setup.HostType = typeof (BrowserScriptHost);
  12. setup.DebugMode = true ;
  13. var runtime = new ScriptRuntime (setup);
  14. var engine = runtime. GetEngine ( "IronPython" );
  15. var scope = engine.CreateScope ();
  16. const string init = @ " import clr clr.AddReference ('System.Windows.Browser') from System.Windows.Browser import *";
  17. ScriptSource initSource = engine.CreateScriptSourceFromString (init, SourceCodeKind.Statements);
  18. initSource.Execute (scope);
  19. var script = engine.CreateScriptSourceFromString (source, SourceCodeKind.Statements);
  20. script.Execute (scope);
  21. return scope;
  22. }
  23. }
  24. }
* This source code was highlighted with Source Code Highlighter .
Run the application and see:

image






')

Finally

Here we have got our own Gestalt, the most interesting thing is that I looked at its source and found about the same implementation. My project can be found at http://code.google.com/p/mygestalt/ . Thanks to all!

Source: https://habr.com/ru/post/65962/


All Articles