python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.27k stars 581 forks source link

Passing File between Python and HTML frontend in single distributable binary #434

Open LukasZweifel opened 3 years ago

LukasZweifel commented 3 years ago

Hello, First, thanks for providing this library! Here is what I do: I'm creating a file in the python code inside the web folder. This file is then used in the gui by a function of a JavaScript library. I just pass the path to the file to that function. My issue: This works all fine as long as I don't package the application with PyInstaller into one distributable binary (as you've kindly described in your readme). If I do that, then the frontend cant access the file. I guess the whole frontend runs in some kind of virtual environment with an own file system. The python code writes the file into the normal file system though. My question: Is there a way to access that virtual env from inside python? Or is there another way I should achieve this?

lnilya commented 2 years ago

Ran into the same problem. My solution is to do this:

#Initialization 
eel.init('build')
eel.start('index.html',port=1234,host='localhost')

...

@eel.expose
def generateAndReturnImage():

     #This will return a very long cryptic path in some tmp folder 
    root_path = eel._get_real_path('build')

    ...

    #saving the new file into the root path 
    imageio.imsave(root_path +'/img.jpg', imgarray) 

    return "http://localhost:1234/img.jpg"

The underlying library, bottle, will create a valid HTTP response and allow this image to be loaded without any problems.

Hope that helps!

lnilya commented 2 years ago

Check the repo I created https://github.com/lnilya/eel-react-typescipt-scss . It is based on react, but the principle is the same.