how to create a desktop app that can run python

Viewed 1328

I made a UI using html and css that takes input as file path. What I need is now to enable my python file to take that path name and run the python script on the input file. How can I achieve this? I saw there is something called eel. but I need this app to be distributed so everyone who installs wont have a chrome installed to run in eel. Please provide me with different alternatives to link these two. I'm quite new to web app.

1 Answers

After some research in Eel and PyInstaller, I found it's quite easy. I tested these instructions in Windows and the example code is from Eel GitHub.

Environment Setup

  • Install pipenv package to setup a virtual environment:
    $ pip install pipenv
    
  • Install eel, pyinstaller to virtual environment:
    $ pipenv install eel pyinstaller
    
  • Due to this issue, we need to install setuptools==44.1.0:
    $ pipenv install setuptools==44.1.0
    

Project Setup

  • Here is the file structure:
    .
    ├── hello.py
    └── web
        ├── favicon.ico
        └── hello.html
    
  • The content of hello.py is:

    import eel
    
    eel.init('web')
    
    @eel.expose
    def say_hello_py(x):
        print('Hello from %s' % x)
    
    say_hello_py('Python World!')
    eel.say_hello_js('Python World!')
    
    eel.start('hello.html', size=(300, 200))
    
  • The content of hello.html is:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Hello, World!</title>
    
            <script type="text/javascript" src="/eel.js"></script>
            <script type="text/javascript">
            <link rel="icon" href="favicon.ico" type="image/x-icon"/>
            eel.expose(say_hello_js);
            function say_hello_js(x) {
                console.log("Hello from " + x);
            }
    
            say_hello_js("Javascript World!");
            eel.say_hello_py("Javascript World!");
    
            </script>
        </head>
    
        <body>
            Hello, World!
        </body>
    </html>
    

Building

  • Run following command:
    $ pipenv run python -m eel hello.py web --onefile --noconsole
    
  • You will see a hello.exe under dist folder.

Any more detail you can read the documents of Eel and PyInstaller.

but I need this app to be distributed so everyone who installs wont have a chrome installed to run in eel.

I think you can use Edge in Eel since Edge is installed in every Windows 10, or you may need to use other python GUI framework. Since you write you UI in html and css, I think is may not be possible to distribute without any browser.

Related