Ruby |ブラウザーでPythonを実行する

なんで?

sidebar_gestalt JavaScriptのWebアプリケーションでクライアントパーツを作成するのが一般的です。 私は、非常に喜んでいるほとんどの開発者がこのルールから逸脱し、お気に入りのサーバーサイド言語を使用したと考えています。 MIX Onlineチームは、動的言語愛好家にこの機会を提供しました。これについての詳細は、「 Gestaltプロジェクト-クライアント側HTMLでRuby、Python、およびXAMLを直接記述する 」を参照してください。 以下では、似たようなものを自分で作成することを提案します。

何で?

いくつかの例を見て、GestaltがSilverlight + Dynamic Languages Runtimeに実装されていることが明らかになりました。 それから私はあなた自身の手で何かを作成することがどれほど難しいかを理解しましたか? すぐに解決策:SilverlightはDOMと対話できるので、コードを取得して解釈できます-それは小さなことです-実装です。

さあ始めましょう

必要なもの:

コーディングしましょう

image Visual Studioを開き、Silverlightアプリケーションを作成し、プロジェクトに「mygestalt」という名前を付けます。 そんなに書く必要はないことを実感しました。実験的なClient-Scriptとそのインタープリターが必要になります。 Silverlightをホストするページ(私の場合はmygestaltTestPage.aspx)を開き、そこにクライアント側のPythonコードを追加します。 次のようになります。
<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 .



次に、MainPage.xaml.csに移動して、スクリプトを検索します。




  1. System.Linq を使用します。
  2. System.Windows.Browser を使用します。
  3. 名前空間 mygestalt
  4. {
  5. パブリック 部分 クラス MainPage
  6. {
  7. パブリック 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. }
*このソースコードは、 ソースコードハイライターで強調表示されました。
まあとPythonEngineの実装:




  1. Microsoft.Scripting を使用します。
  2. Microsoft.Scripting.Hosting を使用します。
  3. Microsoft.Scripting.Silverlight を使用します。
  4. 名前空間 mygestalt
  5. {
  6. パブリック スタティック クラス PythonEngine
  7. {
  8. public static ScriptScope Run( 文字列ソース)
  9. {
  10. var setup = Configuration.LoadFromAssemblies(Package.GetManifestAssemblies());
  11. setup.HostType = typeof (BrowserScriptHost);
  12. setup.DebugMode = true ;
  13. var runtime = new ScriptRuntime(セットアップ);
  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(スコープ);
  19. var script = engine.CreateScriptSourceFromString(source、SourceCodeKind.Statements);
  20. script.Execute(スコープ);
  21. 戻りスコープ;
  22. }
  23. }
  24. }
*このソースコードは、 ソースコードハイライターで強調表示されました。
アプリケーションを起動し、以下を確認します。

image













結論として

そこで、私たちは独自のGestaltを入手しました。最も興味深いのは、ソースコードを見て、そこで同じ実装を見つけたことです。 私のプロジェクトはhttp://code.google.com/p/mygestalt/にあります 。 みんなありがとう!



All Articles