简体   繁体   English

如何将数据从Java小程序传递到客户端python脚本?

[英]How to pass data from Java applet to client side python script?

I have a python script which does various number-crunching jobs and now need to put a graphical front-end on it (a molecular editor in which a user draws a compound to pass to the script) to allow others in the lab to use it more easily. 我有一个Python脚本,可以执行各种数字运算,现在需要在其上放置图形前端(分子编辑器,用户在其中绘制化合物以传递给脚本),以允许实验室中的其他人使用它更容易。 The available editors are Java applets, standalone Java or written in JavaScript. 可用的编辑器是Java applet,独立Java或用JavaScript编写。

I would greatly appreciate any advice on the best strategy to put the editor and script together. 对于将编辑器和脚本组合在一起的最佳策略的任何建议,我将不胜感激。 I really don't want to get into client/server arrangement and envisage a self-contained install on each machine ie "client only". 我真的不想进入客户端/服务器的安排,并设想在每台机器(即“仅客户端”)上进行独立安装。 Is it better to start off in javascript and pass the output in some way to python and then run the python script from within js. 最好从javascript开始,然后以某种方式将输出传递给python,然后从js中运行python脚本。 Or (preferably) is there some way to run applets (or standalone java) within python eg in Tkinter or some other GUI and "on-click" take the output from the editor into the python script for the crunching bit. 或者(最好)有某种方法可以在python中运行小应用程序(或独立的java),例如在Tkinter或其他GUI中,并且“单击”将编辑器的输出带到python脚本中进行处理。

Thanks for any pointers - book chapters, useful projects/libraries and links to examples would be great. 感谢您提供任何指导-本书的章节,有用的项目/库以及示例链接都将非常有用。

The best solution for this problem is surely Jython ( http://www.jython.org/ ). 最好的解决方案肯定是Jython( http://www.jython.org/ )。 With this you can call your python code from Java and even use the Java API in python (ie create a java object and return it). 这样,您可以从Java调用python代码,甚至可以在python中使用Java API(即创建一个Java对象并返回它)。

The only downside is that it only supports Python 2 (and that you get a bit longer startup times). 唯一的缺点是它仅支持Python 2(并且启动时间更长)。 So if you wrote your script in Python3 you would need to port it (depending on what exactly you're doing that may be a bit of work). 因此,如果您使用Python3编写脚本,则需要将其移植(取决于您正在执行的操作可能会有些麻烦)。

If you go for JavaScript I'm assuming it would run in a browser and therefore to communicate with anything else it would make HTTP requests (eg POST or GET URLs), so the client/server arrangement is unavoidable. 如果您使用JavaScript,我假设它会在浏览器中运行,因此与其他任何设备进行通信都会发出HTTP请求(例如POST或GET URL),因此客户端/服务器的布置是不可避免的。

Therefore you would need to turn your Python code into a web application, using something like CherryPy to map URLs to your methods. 因此,您需要使用CherryPy之类的工具将Python代码转换为Web应用程序,以将URL映射到您的方法。 You could then pass data around using JSON notation. 然后,您可以使用JSON表示法传递数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM