r/learnpython • • Apr 04 '24

I was so euphoric starting python but was disappointed with gui tools

I have a background on 20+ years of coding java/php/swift/Js etc. And i was so excited to know more abt tensors and neuron networks, so i finally decided to dig into python which i always found ugly (too used to the C coding style) But i was happy and found that it works!! Panda library makes arrays like an excel sheet. And lot of cool stuff.

Of course i directly went into the object oriented style and making my own packages and classes.

And very quickly i wanted to make GUI as i was tired of running my code in console and writing parameters by hand… and there is when things started to get disappointing.

After many readings i tried the pyside6 library, wich was working but dunno why it was a variant of pyqt and there were some licensing weird details. But i figured out how to make it run.

Then, i of course came to the point where i need my gui to make async calls for some cpu intensive tasks. And this was hell… no documentation, no straight forward way to make it work. And especially so many libraries exists that are abandonned and not maintained wich start making me feel i was on the bad path.

And in this trial and error step i discovered that python is basically mono-cpu language and since it’s interpreted it makes it half the speed of the same php code and up to 500 slower than a C code. (Something i discovered when i found out Cython).

Then all of the sudden i was confused, why people chose this language for neuron networks wich is one of the most cpu intensive coding thing??

I was also digging pytorch and the CUDA library and also found that everything work in some sort of tweaks here and there (wich is nornal since the core of python is moni-cpu).

And finally, i came accross the venv folder, (i started with conda) and realized this folder can reach many gigs of bytes, it reminded me of the node_module in js coding. And there knew that it would be a hell to make a lightweight portable exe file out of my python script. While it was one of the main motivation to start python is to be able to build offline desktop apps

So thats where i am. I hope i am wrong with all the disappointments i listed and that i can find my initial enthusiasm for coding python.

All of your comments are welcomed

UPDATE: Thx for all your inputs it really helped a lot. I figured out that PySide6 used natively async threads with QThread and i have tested it and seems great. Ui does not freeze and the code could detect all the 16cores of my macos and run parallel tasks on them. I am going to stress test the tool to check its limitations. Pyinstaller could generate a 60mb one executable file that runs a ui with all the pyside6 code.

Tutorial found here if anybody is interested: https://realpython.com/python-pyqt-qthread/

7 Upvotes

35 comments sorted by

45

u/HunterIV4 Apr 04 '24

Then all of the sudden i was confused, why people chose this language for neuron networks wich is one of the most cpu intensive coding thing??

Because the neuron network code isn't running on Python.

Essentially, most ML usages of Python use the language as the "glue" to organize and call a bunch of library functions, and most of those library functions are running compiled code (usually C or C++).

The reason why Python is popular is due to fast iteration, meaning that you can easily change around your code structure and get new functionality without messing with a lot of extra work fighting the compiler and language syntax. "It just works (TM)!"

If you are running CPU or GPU intensive tasks directly in Python, you are doing something very, very wrong.

4

u/Equivalent_Style4790 Apr 04 '24

So thats why ML libraries can make use of CUDA cores and the python caller script can only run on 1 thread? So basically python is some sort of controller?

In fact i come from android app development, and in android there is a rule where it’s forbidden to run io tasks on the main thread (that handles the ui) to avoid lagging. And i tried to achieve this with pyside and making an async task with asyncio to avoid making the gui hangs while i make io requests (mostly tried to open xls files with xlrd lib and read cells from it) but the gui hanged anyway until the io tasks finished.

7

u/HunterIV4 Apr 04 '24

I haven't used pyside specifically but every GUI I've used in Python has options for async. For my use it was tkinter with threading, never had any problems, but I also wasn't looking for very pretty GUIs.

4

u/Equivalent_Style4790 Apr 04 '24

Indeed tkinter isnt trying to look sexy but does the job and is lightweight. Since my app doesn’t need to look that much sexy if u state that tkinter does good threading then it would worth a try. Thx 🙏

2

u/freddwnz Apr 05 '24

Tk inter doesn't do threading. You do the threading.

15

u/[deleted] Apr 04 '24

Get used to not using UIs honestly. UIs are what you write for other people. Programmers, internally, will almost always find scripts and programs to be run from a terminal more useful.

Also, the general model with GUI libraries is kind of dying. 90% of the time time writing a website is the easier thing to do because of how mature HTML/Javascript are as ecosystems. It's why so many apps are now electron apps (i.e a website running in some form of locked down chrome browser).

1

u/Equivalent_Style4790 Apr 04 '24

Js can call python scripts from withing that locked down chrome browser with no slowness?

7

u/MarchColorDrink Apr 04 '24

Js frontend calls api endpoint in Python backend

-1

u/Equivalent_Style4790 Apr 04 '24

So i must create a python backend server to make this work? Isn’t it a bit heavy? But it’s still a good news since i master webdev.

3

u/[deleted] Apr 05 '24

Honestly it sounds like you should be using jupyter notebook. I'm not sure why you want to enter parameters into an ML model using a GUI and notebooks allow you to interactively run code so changing parameters will be much easier each time you want to run.

1

u/Equivalent_Style4790 Apr 05 '24

My final goal as a small freelance dev is to deliver executables with gui. But im facing the rough truth little by little. Yesterday i tried pyinstaller, my dist folder had many gb ! 🤣.

1

u/100721 Apr 05 '24

With flask and Django it’s not heavy. You can get a server running very quickly. That’s the whole point of python, nothing is heavy and nothing is blazingly fast.

2

u/Equivalent_Style4790 Apr 05 '24

It’s rough how u sum it up 🤣

4

u/RooCoder Apr 04 '24

The solution: Use flask to write a Model-View Controller / API. It is soo small it can be ran as a desktop app. The GUI is HTML/CSS. It will work on Windows/Mac/Linux.

There's even projects like flaskwebgui and flask-desktop now but I have managed it without needing them.

What is it with python and these rubbish GUI tools like tkiner PyQt. No other language uses them since the 90s.

0

u/Equivalent_Style4790 Apr 05 '24

Sure bro abt that? Qt seems to be a badass framework , dont really know what it does lol. It got some abstraction layer that would make ur code run on rapsberry. But gonna dig flask too.

1

u/[deleted] Apr 05 '24

QT is the KDE windowing toolkit. The only reason it's so heavily used is because it's the only library for this that's really cross platform and is moderately less terrible than GTK. Obviously WinUI only runs on windows and whatever the mac one is only runs on mac. Java AWT and Swing are both cross platform but obviously not cross language.

Don't get it twisted, these libraries still have their place. If you're developing Kate or Dolphin or the KDE settings menu or the standard menu-bar for the latest release of KDE Neon then your hand is forced. Also if you need a low latency, complex GUI app like a text editor or filezilla where you would need to make a lot of internal HTTP requests then a website is not a good idea and then you're also constrained.

One issue with QT apps is that they're so baked into the KDE ecosystem that if you run on a different platform it will typically look really really ugly because it'll have no system wide settings to inherit. You can try to theme it yourself but it's a lot more work getting things aligned and pretty with QT than it is HTML/Javascript. It's also typically a lot more code and the organisation code is a lot more complicated and requires a lot of polymorphic inheritance with deep class trees and mixins. HTML is also much easier to read and reason with and you can always throw it into a web browser (before packaging it as an electron app), press F12 and tinker with it live so it has a very fast cycle time.

4

u/ElectricalAd3189 Apr 05 '24

Tbh you are absolutely right. Python is not good/efficient at anything. However it trumps every other language in adoption and popularity for some mysterious reason. But the price is paid in efficiency. Now there is billion dollars to be made if someone can figure out how to run this sloth go fast. PPL are actively researching hw solutions for this problem

1

u/Equivalent_Style4790 Apr 05 '24

Your comment suggest to keep the faith lol. I have read that GIL will be dropped anytime soon.

3

u/Educational_Abies263 Apr 04 '24

Check streamlit

It has some limitations, but for certain stuff, it's genious

1

u/Equivalent_Style4790 Apr 04 '24

Definitely going to make a test. But this library provides a simple server or converts the py script output to some html?

2

u/Educational_Abies263 Apr 04 '24

It creates a server which hosts some magic javascript
It's very fast to get it running

5

u/Anomie193 Apr 04 '24

And in this trial and error step i discovered that python is basically mono-cpu language and since it’s interpreted it makes it half the speed of the same php code and up to 500 slower than a C code.

This isn't because it is "interpreted" but because the default implementation of the Python spec (CPython), which has both an (implicitly) compiled step to byte-code and an intepreted step in a Virtual Machine, has a Global Interpreter Lock to accommodate for the fact that it isn't inherently a thread-safe implementation. You can have interpreted implementations of languages that are thread-safe and don't require a GIL, though. 

This might change for CPython over the course of the next decade or two anyway.

https://peps.python.org/pep-0703/

1

u/xmrn Apr 04 '24

And in this trial and error step i discovered that python is basically mono-cpu language and since

Python comes with the multiprocessing library, which makes it quite easy to use multiple processes/ cores. You can start the processes yourself use a pool and communicate between them.

1

u/Equivalent_Style4790 Apr 04 '24

U mean asyncio or another library?

3

u/xmrn Apr 04 '24

No its just called multiprocessing and spawns new processes. There would be different ways how you can implement async behaviors with that.

https://docs.python.org/3/library/multiprocessing.html#multiprocessing-start-methods

Btw for gui you might have a look at pywebview. A bit like electron but the backend is in python, frontend is hmtl/js/react and you can have async behavior. And you can have nice results visually

1

u/Equivalent_Style4790 Apr 04 '24

Wow. Thanks. Brings back hope.

1

u/Dylan_TMB Apr 05 '24

You probably want some frontend dash boarding tool like Dash, streamlit etc.

You mention freelancing but you'll never deliver a product as an all in one python program. Itlls almost always be a SaS tool that has a web front end. I'd probably start with streamlit

1

u/Equivalent_Style4790 Apr 05 '24

Im reaching little by little this conclusion also.

-4

u/member_of_the_order Apr 04 '24

I'm not in ML myself, but the answers I've seen when others have asked on this sub is that Python is very good at prototyping and making fast changes, but obviously not so good for polished (fast, good UI, etc) products.

ML is very new, so most of the libraries are still in the "unoptimized prototype" stage, which - as has been established - Python is very good for.

If you want a more optimized ML library, say, in C++, you would be doing the world a big favor by creating that library! As far as I've heard, that work has yet to be done for no other reason than stuff is still in the research phase.

13

u/Doormatty I get paid to write Python! Apr 04 '24

but obviously not so good for polished (fast, good UI, etc) products.

That's completely untrue.

ML is very new, so most of the libraries are still in the "unoptimized prototype" stage, which - as has been established - Python is very good for.

Also completely untrue. All the standard python ML libraries do their heavy lifting in FORTRAN/C++ already.

0

u/Equivalent_Style4790 Apr 04 '24

U mean the pyc files?

7

u/Doormatty I get paid to write Python! Apr 04 '24

Nope, pyc files are just "compiled" python bytecode.

Python libraries can be written in C++ without any problem.

1

u/Equivalent_Style4790 Apr 04 '24

So i guess pythorxh and other ml libraries must be writted in c++ ? Just wondering. But still using 1 cpu core anyway? Because of GIL? Or GIL isnt a problem with c++ written libraries? Im sorry but im still a bit confused

5

u/JojainV12 Apr 04 '24

When you are writing a python lib in C or C++ you can release the GIL. That means that your computation in C can be multithreaded and when you hand over the result to python it will run the python code in a single thread until it calls multithreaded C code again

1

u/Equivalent_Style4790 Apr 04 '24

Okey then. This start making sense. So python is some sort of unique thread that holds the console app or the gui app then makes C or C++ handles the heavy work under the hood. It’s closer to bash or .bat scripting than a regular programming language.