Recent

Author Topic: Lazarus to write a UI around Python code - good idea?  (Read 6917 times)

graemex

  • New Member
  • *
  • Posts: 12
Lazarus to write a UI around Python code - good idea?
« on: July 10, 2016, 10:52:06 am »
I know that this is something other people have done, and projects exist to support it.

I an new to Pascal, and I do not have much experience of GUI development.

I want an initially fairly simple GUI that I can gradually enhance.

The whole thing seems a bit backwards to me (using the fast compiled language for the UI, and the slow language for everything else), but I see other people have done it and there is some information about doing it.

I really want the easiest approach, and Lazarus seems to be a lot easier to work with than any similar tool for Python, so I am trying to weigh that up against the additional difficulties caused by using another language. That I would have to learn Pascal to do it is not a huge problem, because I am interested in learning Pascal anyway, and I am going to have to learn one of the (fairly complex) GUI libraries that can be used with Python if I use Python.

Any hints? Any problems I should be aware of before going down this route?


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3854
  • I like bugs.
Re: Lazarus to write a UI around Python code - good idea?
« Reply #1 on: July 10, 2016, 12:01:32 pm »
Not a good idea IMO.
You must struggle with passing data between the languages, with 2 development environments and finally with a run-time environment.
The final balance of time savings goes on negative side.
Just port your code to Pascal and you are good. If there is too much code to be ported then this is not an option of course.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8273
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Lazarus to write a UI around Python code - good idea?
« Reply #2 on: July 10, 2016, 01:28:01 pm »
The whole thing seems a bit backwards to me (using the fast compiled language for the UI, and the slow language for everything else), but I see other people have done it and there is some information about doing it.
Yep, you're correct here. "Normal" people would do UI in a dynamically interpreted language, with core functionalities and GUI components written in a statically compiled language. It is uncommon to have fast UI with slow background operation compared to fast background operation and slow UI.
Any hints? Any problems I should be aware of before going down this route?
Just like writing 2 (or more) languages app: understand the contract between them and you're all set. FYI, Python interface to statically compiled language is considered complex. I'd use Lua if I have to combine dynamic and static language due to its simple interface (and we have the bindings in default installation), not to mention its performance if you use LuaJIT which is marginally faster than even PyPy.

vfclists

  • Hero Member
  • *****
  • Posts: 827
    • HowTos Considered Harmful?
Re: Lazarus to write a UI around Python code - good idea?
« Reply #3 on: July 10, 2016, 01:34:40 pm »
You can do it, but it requires good knowledge of the language as well as Lazarus/FreePascal.

A good example of what you are thinking of is VCL Lua.

https://sourceforge.net/projects/vclua/

Another one is pLua - https://github.com/felipedaragon/pLua-XE which has gone into developing https://github.com/felipedaragon/sandcat which is a Delphi project.

For simpler starting libraries that give more insight into the process there is stuff like https://github.com/malcome/Lua4Lazarus, which give some insight into how Lua links with Lazarus.

On the Python level there is http://wiki.freepascal.org/Python4Delphi which has had some FPC related updates at  https://github.com/Alexey-T/ATSynEdit and https://github.com/tangentstorm/py4d

Given that the developer of VCLua managed to wrap it all into a DLL and make the whole thing accessible directly from the command line without any hint of Lazarus involved, I think that is your best bet, or at least your guide to insight into the process.

You will also need some more info into the Lazarus RTTI and developments in the RTTI branch will help.

If you are feeling brave enough you can try implementing the graphics part in fpgui, which doesn't have all the baggage of the LCL but has fewer controls implemented.
« Last Edit: July 10, 2016, 03:07:41 pm by vfclists »
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.4.4 /FPC 2.6.4

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Lazarus to write a UI around Python code - good idea?
« Reply #4 on: July 10, 2016, 04:08:32 pm »
Any hints? Any problems I should be aware of before going down this route?

What problem are you trying to solve? I don't have a clue as to what you're thinking of doing.

Do you have a large existing Python codebase and you need to embed the Python interpreter into a GUI (Pascal) desktop app? See references above. This is a common use for Python. For example, QGIS includes Python in its Windows installer since Python is required for its many plugins (and Python is not installed on Windows by default the way it is on Mac and Linux).

Do you have Python skills such that you prefer to write most of the code in Python? If so, I wouldn't bother with Pascal.

Again, what is _essential_ about what you've proposed (Pascal GUI calling Python code) that forces you down this route?

Note that if the Python code does just one or two big things, you can just shell to the Python interpreter from Pascal rather than embed the interpreter. That's easy to do and is already done in at least one package for Lazarus:

https://sourceforge.net/projects/qxotica/

When you choose File | New | qooxdoo Javascript App, the Pascal package runs the qooxdoo generate.py script to create the app skeleton. Rewriting generate.py in Pascal would be a fool's errand.

-Phil

sfeinst

  • Full Member
  • ***
  • Posts: 186
Re: Lazarus to write a UI around Python code - good idea?
« Reply #5 on: July 10, 2016, 04:36:12 pm »
As others have stated, a lot depends on what you expect the GUI to do in relation to the Python code.  For example I wrote a GUI wrapper around some ImageMagick scripts (yes I know there is Pascal Magick to do the whole thing in Pascal - but the scripts are already written).  The scripts take command line params and do not return any data.  So creating a quick GUI was easy.

You should state how complicated the data passing will be between the scripts and the GUI.  And how complicated the scripts themselves are.

graemex

  • New Member
  • *
  • Posts: 12
Re: Lazarus to write a UI around Python code - good idea?
« Reply #6 on: July 11, 2016, 09:10:29 am »
Thanks for your replies.

I now think it is the wrong tool for the job - my thinking was biased by a wish to find something to use as a project to learn Pascal with. I should wait until something that is a better fit comes up.

alaa123456789

  • Jr. Member
  • **
  • Posts: 85
Re: Lazarus to write a UI around Python code - good idea?
« Reply #7 on: August 09, 2020, 07:38:11 pm »
You can do it, but it requires good knowledge of the language as well as Lazarus/FreePascal.

A good example of what you are thinking of is VCL Lua.

https://sourceforge.net/projects/vclua/


could you please show us how vcl lua works?

thanks

Handoko

  • Hero Member
  • *****
  • Posts: 3850
  • My goal: build my own game engine using Lazarus
Re: Lazarus to write a UI around Python code - good idea?
« Reply #8 on: August 09, 2020, 07:47:22 pm »
Did you mean some demo?
The demo is bundled with the source code.

lainz

  • Hero Member
  • *****
  • Posts: 3763
  • Leandro Diaz
Re: Lazarus to write a UI around Python code - good idea?
« Reply #9 on: August 09, 2020, 08:02:23 pm »
I've seen a tool like pgAdmin 3 for postgresql having an ui that you can made with Lazarus to be changed entirely in version 4 to a localhost web application.
Like html and js for the ui and say any language for the backend.

alaa123456789

  • Jr. Member
  • **
  • Posts: 85
Re: Lazarus to write a UI around Python code - good idea?
« Reply #10 on: August 09, 2020, 08:36:54 pm »
Did you mean some demo?
The demo is bundled with the source code.

i downloaded it , it has only dll file

thanks

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Lazarus to write a UI around Python code - good idea?
« Reply #11 on: August 09, 2020, 08:51:20 pm »
Did you mean some demo?
The demo is bundled with the source code.

i downloaded it , it has only dll file

thanks
There seems to be a separate demo directory, here: https://sourceforge.net/projects/vclua/files/demos/

alaa123456789

  • Jr. Member
  • **
  • Posts: 85
Re: Lazarus to write a UI around Python code - good idea?
« Reply #12 on: August 09, 2020, 09:12:09 pm »
i downloaded the demo file , but how to run these demos, it seems interesting subject

thanks 

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Lazarus to write a UI around Python code - good idea?
« Reply #13 on: August 09, 2020, 09:21:10 pm »
i downloaded the demo file , but how to run these demos, it seems interesting subject
I have no experience whatsoever with vclua, not do I have any intend to test it  :)

However, logic seems to dictate:
- install lua
- install vcl dll in the appropriate directory
- run demo-script with lua.

How you can run a script with lua is mentioned in the lua documentation and how you can install the vcl lua library in the appropriate directory is mentioned in the readme(s) of vclua. Just make sure you have the correct library for your system, or you can compile the vclua project yourself (you have to download the sources-archive for that, not the distribution archive)

Warfley

  • Sr. Member
  • ****
  • Posts: 309
Re: Lazarus to write a UI around Python code - good idea?
« Reply #14 on: August 10, 2020, 03:28:04 pm »
I did this for a few Machine Learning programs, because there Python is king and porting the code to Lazarus would be completely pointless.

There are basically 2 ways for easy communication with a python program:
1. Streams, the easiest ones being stdin stdout and stderror, which every process has naturally. So simply start the python script and supply all information in a format (like csv or json) via stdin and read outputs in such a format via stdout.
Besides these standard streams you can also simply open a TCP socket or use fifos.
2. Program arguments and files.
For my ML program I already had a python program that would take a number of parameters via the command line and produce output files, so I simply took the UI inputs and used them to call the process with them as arguments, and once the process finished I just read the output files.

 

TinyPortal © 2005-2018