Recent

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

danilomo

  • Newbie
  • 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

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • 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

  • Newbie
  • 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

  • Hero Member
  • *****
  • Posts: 515
  • 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 Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

danilomo

  • Newbie
  • 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: 14373
  • Sensorship about opinions does not belong here.
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.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
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 »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
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.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

danilomo

  • Newbie
  • 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: 6686
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & 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: 1302
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.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:
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

  • Newbie
  • 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