Recent

Author Topic: Creating a GUI layer for an existent command line Python application  (Read 3144 times)

danilomo

  • New member
  • *
  • Posts: 5
Hi,

I have two command line applications (written in Python) that I would like to extend with a GUI layer. After some research I came to the conclusion that Python sucks hard for this task. Then I remembered my old friend Lazarus I used 10 years ago (I took a Delphi course and learnt Lazarus as well). In this 10 years period I switched to Java/Python and did not write a single line in Pascal.

One application is straightforward and can be rewritten completely in Object Pascal, but the second one relies on matplotib/pandas/etc. so rewriting it is not an option. I just want a GUI layer for it.

What are my options?

My initial idea is exposing the core functionality as a set of webservices, but it'll be a bit annoying.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9286
  • FPC developer.
Re: Creating a GUI layer for an existent command line Python application
« Reply #1 on: July 03, 2018, 10:14:18 am »
It will probably boil down to:

1) How do you execute an python script ? Since python generally doesn't create executables, you either need some way to start it over the interpreter, or generate EXEs for them.

2) how do you pass data to and from the script? Commandline parameters, pipes?

I hope I gave you some ideas.

danilomo

  • New member
  • *
  • Posts: 5
Re: Creating a GUI layer for an existent command line Python application
« Reply #2 on: July 03, 2018, 10:23:01 am »
I structure my CL applications this way:

$<command> <subcommand> <arguments>

Consider this hypothetical application, a simple TODO list. I would have the following commands:

$./todo.py add -due today -task "Clean your room, bucko"
Task created with id 12345.

$./todo.py list
Number   id            Task                                    Due date
1             12345      Clean your room, bucko     today

$./todo.py done 12345
Task 12345 done!



x2nie

  • Sr. Member
  • ****
  • Posts: 488
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Creating a GUI layer for an existent command line Python application
« Reply #3 on: July 03, 2018, 10:37:31 am »

If you mean that your didn't yet found any, ...there are GUI layer for python already.
* pyGTK --> using GTK+ for gui representation [1]
* pyQT --> using Qt [2]
* wxPython --> using wxWidgets  [3]




[1] https://en.wikipedia.org/wiki/PyGTK
[2] https://wiki.python.org/moin/PyQt
[3] https://wxpython.org/pages/overview/
When you were logged in, you can see attachments.
Lazarus Trunk @ Windows7 64bit, XP 32bit, Debian under VirtualMachine

danilomo

  • New member
  • *
  • Posts: 5
Re: Creating a GUI layer for an existent command line Python application
« Reply #4 on: July 03, 2018, 10:42:21 am »

If you mean that your didn't yet found any, ...there are GUI layer for python already.
* pyGTK --> using GTK+ for gui representation [1]
* pyQT --> using Qt [2]
* wxPython --> using wxWidgets  [3]




[1] https://en.wikipedia.org/wiki/PyGTK
[2] https://wiki.python.org/moin/PyQt
[3] https://wxpython.org/pages/overview/

I know those options.

Python is great for web, data science, glue scripts, shell on steroids, etc. but it doesn't shine for GUI. They are sort of ok, but not as good as Lazarus. I just want the best of two worlds, I'm considering using Lazarus as a frontend for a python backend application. I want to experiment with this architecture and see if it is okay.

« Last Edit: July 03, 2018, 10:44:13 am by danilomo »

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
Re: Creating a GUI layer for an existent command line Python application
« Reply #5 on: July 03, 2018, 10:54:28 am »
You should not have any trouble using pipes. Maybe combined with json as an object interchange format.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 664
Re: Creating a GUI layer for an existent command line Python application
« Reply #6 on: July 03, 2018, 11:21:00 am »
I guess, you should read this:
Embedding Python in Another Application.
Python/C API Reference Manual.

As I know, Python.exe itself - is just a wrapper around PythonXX.dll. You can use it directly. You just need header files. You need to find existing ones or convert C/C++ ones. Googling has given me this: Using Python in Lazarus on Windows/Linux.
« Last Edit: July 03, 2018, 11:26:05 am by Mr.Madguy »
30.04.2021 - DynamicData 4.0 is released and migration to it is completed.
It's more Lazarus-friendly, but still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 12 years old.

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
Re: Creating a GUI layer for an existent command line Python application
« Reply #7 on: July 03, 2018, 11:53:05 am »
Yeah, forgot about that. I used to experiment with http://www.atug.com/andypatterns/pythonDelphiTalk.htm but that looks old.
That said there are already many header translations available for FPC and Delphi.

The article is still useful.

danilomo

  • New member
  • *
  • Posts: 5
Re: Creating a GUI layer for an existent command line Python application
« Reply #8 on: February 19, 2021, 04:20:04 pm »
I'm sorry for the very late reply.

I was able to implement a prototype for a Lazarus application that uses the functionalities of a Python program. I call the Python program as a TProcess object, and use TStringList for reading lines from the program's output. I use JSON as message format and everything worked well.

I'm quite pleased with this combination! Lazarus is the best free-as-in-free-beer thing for building desktop applications, and Python is my favorite language. I guess I will create some library in both Python and Pascal to reduce the boilerplate code needed to glue both languages.

Cheers!

MarkMLl

  • Hero Member
  • *****
  • Posts: 2565
Re: Creating a GUI layer for an existent command line Python application
« Reply #9 on: February 19, 2021, 04:38:28 pm »
Python4delphi demonstrates some degree of support for interception Python output etc., but I've not yet investigated the extent to which the GUI side can be "controlled" by what the Python script is doing. I've been fettling that stuff a bit over the last few days, as described at https://forum.lazarus.freepascal.org/index.php/topic,53348.msg394506.html with the motivation being https://forum.lazarus.freepascal.org/index.php/topic,52849.0.html

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Edson

  • Hero Member
  • *****
  • Posts: 1148
Re: Creating a GUI layer for an existent command line Python application
« Reply #10 on: February 19, 2021, 04:57:25 pm »
If you want to talk to Python, or any other process, you can use my library: https://github.com/t-edson/UnTerminal

It's a wrapper for TProcess and can send commands and receive the output using events.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

alaa123456789

  • Full Member
  • ***
  • Posts: 167
Re: Creating a GUI layer for an existent command line Python application
« Reply #11 on: February 19, 2021, 05:04:09 pm »
hi ,

watch this toutrial as start ,maybe you can have some ideas
https://www.youtube.com/watch?v=WREEoECCMy0

thanks

danilomo

  • New member
  • *
  • Posts: 5
Re: Creating a GUI layer for an existent command line Python application
« Reply #12 on: February 25, 2021, 11:51:21 am »
If someone sees this topic in future, coming from Google with the same question I had initially, I want to share what I was able to achieve so far:

https://github.com/danilomo/PyLernkarten

There is a Python command line application that works like an interactive shell and there is a Lazarus GUI application which starts this program with a TProcess and sends commands to it via input/output.

Only works on my machine at time of writing of this comment, hopefully I'll make it robust enough later.

 

TinyPortal © 2005-2018