Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Electron as front end #57

Open
ChrisKnott opened this issue Jun 20, 2018 · 47 comments
Open

Using Electron as front end #57

ChrisKnott opened this issue Jun 20, 2018 · 47 comments

Comments

@ChrisKnott
Copy link
Collaborator

ChrisKnott commented Jun 20, 2018

I mainly use Eel to make prototypes or personal/internal tools, where polish is not that important, but if you want to make a more professional looking app with HTML/JS frontend, and Python backend, you can use Electron as the browser. This will give you total control over things like right click menus, menubars etc.

I have added a new custom mode which just runs whatever command you provide in args. In the future I might add more "first class" support for Electron.

The absolute smallest possible Electron app needs two files, a package.json file that looks like...

{
  "main": "main.js",
  "devDependencies": {
    "electron": "^2.0.0"
  }
}

and a main.js that looks something like...

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL('http://localhost:8000/start_page.html');
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  app.quit()
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

Then, in your python script, you need to specify options something like...

options = {
	'mode': 'custom',
	'args': ['/usr/local/bin/electron', '.'],
	(...)
}
eel.init('web')
eel.start('start_page.html', size=(800, 600), options=options)
This was referenced Jun 20, 2018
@abulka
Copy link

abulka commented Jun 21, 2018

Thanks for this work - I'll check it out.

My recent software product is now complete. It definately needed menus, a standalone build of chrome and other electron features so I had no choice but to jump ship and use https://github.com/fyears/electron-python-example. But I'll re-check out the feasibility of eel with electron for my next project.

@04fsnape
Copy link

04fsnape commented Jun 21, 2018

@ChrisKnott So I went ahead and created my own electron app (without eel). Then, I put gui.py in that folder:

import eel

options = {
	'mode': 'custom',
	'args': ['C:/Users/x/PycharmProjects/adidas/gui']
}

eel.init('html')
eel.start('index.html', options=options)

Here is my main.js:

const {app, BrowserWindow} = require('electron')

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 1206, height: 729, icon:'images/favicon-32x32.png', resizable: false, title: ""})
  mainWindow.setMenu(null);
  mainWindow.loadFile('http://localhost:8000/index.html')

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}
app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

The Electron app works fine when I run it via npm run. However, when I try and run gui.py I get the following error:

Traceback (most recent call last):
File "C:/Users/x/PycharmProjects/adidas/gui.py", line 9, in
eel.start('index.html', options=options)
File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel_init_.py", line 112, in start
brw.open(start_urls, options)
File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\browsers.py", line 39, in open
stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE)
File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\subprocess.py", line 604, in init
restore_signals, start_new_session)
File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\subprocess.py", line 955, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied

This is my file structure within the gui folder:

css

global.css

html

index.html

node_modules
gui.py
main.js
package.json
package-lock.json
renderer.js

Any ideas?

@ChrisKnott
Copy link
Collaborator Author

ChrisKnott commented Jun 21, 2018

args should point to the electron executable. When you run npm start, what does it say in the command prompt as it starts up? Should be something like electron.exe .

@04fsnape
Copy link

04fsnape commented Jun 21, 2018

@ChrisKnott OK so I found the electron exe file.

Now my gui.py file looks like this:

import eel

options = {
	'mode': 'custom',
	'args': ['C:/Users/x/PycharmProjects/adidas/gui/node_modules/electron/dist/electron.exe', '.']
}

eel.init('html')
eel.start('index.html', options=options)

When I run this, it opens an electron browser but it is just blank. When I go to localhost:8000 on my browser, I can see my page but there is no CSS being applied to it...

What am I missing here?

EDIT: When I view source on localhost:8000/index.html and click on the link to my css file (../css/global.css), I get a 404 error. Why is this not showing up?

EDIT2: Changed loadFile to loadURL in main.js. Now it opens the page but again without styling. How can I add all of my folders to localhost?

@04fsnape
Copy link

Also, closing the app doesn't end the python script.

@04fsnape
Copy link

Fixed. I put the html, css, js and images folders in a folder called web, then I did eel.init('web) and eel.start('html/index.html').

Works great now except for the issue with the python script still running.

@ChrisKnott
Copy link
Collaborator Author

To close the Python script just call into it from Electron (in the close handler) then call sys.exit() on the Python side

@04fsnape
Copy link

@ChrisKnott I'll test that in a bit bit right now I'm having another problem. I added a custom title bar and close/minimise buttons which works absolutely fine when I run it via npm start. However, when I run it via eel it shows an old version of my project and nothing will update. I even tried reverting it back in case my title bar code was causing an issue and just changing one letter of an element, but it still shows the old version and nothing will change. I tried restarting my PC but the issue persists.

What's going on here?

@ChrisKnott
Copy link
Collaborator Author

Hmmm not sure really, perhaps you have two copies of the file?

@04fsnape
Copy link

@ChrisKnott I tried it a few hours after and it just worked... Strange.

Also I got the closing to work properly, thanks.

I even made a custom title bar in electron which looks great.

@04fsnape
Copy link

It seems like the issue was caused by electron's caching which can be cleared by going to C:\Users<user>\AppData\Roaming<project>\Cache and deleting the files.

@nealdav
Copy link

nealdav commented Jul 18, 2018

@ChrisKnott Just wanted to say thanks for adding this capability. It's working really well for me!

@ChrisKnott
Copy link
Collaborator Author

Thanks Neal I appreciate it

@nba94
Copy link

nba94 commented Aug 12, 2018

@ChrisKnott Hi Chris, thanks so much for this.

1 question - I'm trying to the above suggestion with Electron, however whenever I run my .py script, instead of launching a new window in Electron, it just adds a new tab to my chrome browser. Otherwise, it just opens a new instance of Chrome.

Seems like it's ignoring the fact that I want to launch it using Electron and uses Chrome instead.

@ChrisKnott
Copy link
Collaborator Author

@nba94 If it's opening a Chrome tab, then eel must be opening a new instance of Chrome instead of Electron. Do you have 'mode': 'custom' added to the options dictionary?

@nba94
Copy link

nba94 commented Aug 15, 2018

@ChrisKnott I do. I have the following as options:

options1 = { 'mode': 'custom', 'port': 8000, 'args': ['/usr/local/bin/electron','.'], }

and this at the end:
eel.start('index.html',options=options1)

Yet it still launches Chrome for some reason.

@ChrisKnott
Copy link
Collaborator Author

Maybe you are still running the old version of eel...?

@nba94
Copy link

nba94 commented Aug 15, 2018

@ChrisKnott Yes.. Now it recognizes it! Thanks and sorry...

@nba94
Copy link

nba94 commented Sep 9, 2018

@ChrisKnott Thanks for your help so far - this is an amazing addition to the module.

I have a question.. When I tried to compile the application using pyinstaller, what is the correct way to include Electron?

Do you include it in datas as a complete .app executable file to be stored within the .app itself? I have tried doing that, but that did not work.

Maybe there is something that I am missing out?

FYI I am on MacOS.

@ChrisKnott
Copy link
Collaborator Author

I think the best way is to use --add-data option to include Electron binaries. There is more info on pyinstaller's help https://media.readthedocs.org/pdf/pyinstaller/cross-compiling/pyinstaller.pdf

These will be extracted to a temp folder when the .app is run. You want to use the path of that temp folder in your args list

@nba94
Copy link

nba94 commented Oct 1, 2018

@ChrisKnott I finally worked it out..

Just in case anyone else will be wondering these are the following steps I took to compile into a single file on MacOS:

  1. Download compiled electron from https://github.com/electron/electron/releases (for me it was electron-v2.0.11-darwin-x64.zip)

  2. Right click -> open contents of the downloaded Electron, create 'app' folder in the Contents -> Resources and move my main.js, index.html and package.json inside.

  3. As Chris mentioned above, --add-data has to be used to include Electron, however for some reason it does not include some of the libraries for Electron to run successfully when compiled, so including Electron and those files as follows:

  • --add-data [include Electron.app that you have downloaded previously]

  • --add-data /Users/[path to downloaded Electron]/Electron.app/Contents/Frameworks/Electron\ Framework.framework/Versions/A:Electron.app/Contents/Frameworks/Electron\ Framework.framework

  • (optional) instead of point 2 it is also possible to just write --add-data for those files required to include inside Electron, following this format:
    --add-data /Users/[path to your main.js]/main.js:Electron.app/Contents/Resources/app

After making sure these files are included everything ran as expected.

FYI In case anyone is planning to compile using --onefile - on MacOS you have to make sure all of the references to any paths in your script are using relative paths. The following function ensures that the path to any file you use in your script that is saved locally is turned into a relative path:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

Even options has to be rewritten using the function in order for --onefile compilation to work correctly, as follows:

options1 = {
     'mode': 'custom',
     'port': 8000,
     'args': [resource_path('Electron.app/Contents/MacOS/Electron'),'.'],
 }

Sorry for the lengthy post, but maybe this will help someone spend way less time than I have on this!

@ChrisKnott
Copy link
Collaborator Author

@nba94 Thanks for the detailed writeup, I'm sure this will help lots of people

@greixs
Copy link

greixs commented Apr 20, 2019

anyone can help on how to add eel.js while using electron ?

@ChrisKnott
Copy link
Collaborator Author

ChrisKnott commented Jun 14, 2019

You now longer have to use 'custom' mode for this, you can say 'electron' in latest version (currently in beta)

You may also want to use;

import eel.browsers
eel.browsers.set_path('electron', 'node_modules/electron/dist/electron')

to force using binary from local folder (this should also help with packaging with pyinstaller).

I have added an example for a minimal electron app. I will improve it when beta is released fully on PyPI.

For now I am going to close this issue as there are too many open atm.

@zphiliam
Copy link

To close the Python script just call into it from Electron (in the close handler) then call sys.exit() on the Python side

Close handler is not in render process ,how to call into python script ? I'm confused

@ChrisKnott ChrisKnott reopened this Jul 31, 2019
@ChrisKnott
Copy link
Collaborator Author

Yes I think it is not as easy as it seems, I will make an example with clean shutdown when I have time, hopefully in w/c 12th August as I have time off work then

@w6tsang
Copy link

w6tsang commented Dec 3, 2019

Yes I think it is not as easy as it seems, I will make an example with clean shutdown when I have time, hopefully in w/c 12th August as I have time off work then

Any updates to this?

@samuelhwilliams
Copy link
Collaborator

Hi @w6tsang - I've taken over primary maintenance of this project for now, and unfortunately I'm not super familiar with this side of the project. I'll try to look into it when I have time - but in the mean time if anyone else comes across this and has more experience and the willingness to share ways that might work, it would be really appreciated.

@ChrisKnott
Copy link
Collaborator Author

I have a project using Electron + Eel 1.0 so I will probably submit fix for this at some point

@omkarbhad
Copy link

Any Updates on Electron + Eel ?
Please include the integration of Electron with Eel in the Documentation

@dragon-slayer875
Copy link

dragon-slayer875 commented Jul 6, 2020

  1. Download compiled electron from https://github.com/electron/electron/releases (for me it was electron-v2.0.11-darwin-x64.zip)
  2. Right click -> open contents of the downloaded Electron, create 'app' folder in the Contents -> Resources and move my main.js, index.html and package.json inside.

Hello @nba94. Did you mean something like this?

image

Or like this?

image

@mo0haned
Copy link
Contributor

anyone can help on how to add eel.js while using electron?
hello @greixs

I was facing the same issue. I found in the examples folder an example for electron + eel that helped me to fix it, my problem was using the HTML file in the main.js file. but eel creates the eel.js in localhost:PORT/eel.js or something like that. but not in the www folder.
so you should change from mainWindow.loadfile('index.html'); to mainWindow.loadURL('http://localhost:8000/index.html); in the main.js file.

also, I found out that using URL ends the python script after closing the electron app. No idea why.

My way to use electron with eel.

  1. install Electron using npm in a folder. it will create "node_modules/Electron/..." folder.

  2. eel searches for "../node_modules/Electron/..." to execute the electron browser. so you should make a sub-folder beside the node_model folder, now your main folder should contain node_modules and app folders

  3. in eel.start function just add mode='electron'

for me I found an error in the electron.py file in the eel in line 18. bat_path was not working. I changed it in the join function to just empty "" and it worked. I'm not sure what it does so I didn't want to send any edit with it.

@luciferamji
Copy link

anyone can help on how to add eel.js while using electron?
hello @greixs

I was facing the same issue. I found in the examples folder an example for electron + eel that helped me to fix it, my problem was using the HTML file in the main.js file. but eel creates the eel.js in localhost:PORT/eel.js or something like that. but not in the www folder.
so you should change from mainWindow.loadfile('index.html'); to mainWindow.loadURL('http://localhost:8000/index.html); in the main.js file.

also, I found out that using URL ends the python script after closing the electron app. No idea why.

My way to use electron with eel.

  1. install Electron using npm in a folder. it will create "node_modules/Electron/..." folder.
  2. eel searches for "../node_modules/Electron/..." to execute the electron browser. so you should make a sub-folder beside the node_model folder, now your main folder should contain node_modules and app folders
  3. in eel.start function just add mode='electron'

for me I found an error in the electron.py file in the eel in line 18. bat_path was not working. I changed it in the join function to just empty "" and it worked. I'm not sure what it does so I didn't want to send any edit with it.

Were you able to package that electron app.
I can run electron app but i am not able to package it

@mo0haned
Copy link
Contributor

mo0haned commented Dec 9, 2020

actually no, I had tried to pack it but it didn't work, but also I didn't try again because I was going to use it in a developing env. so it was ok for me

@raphaelnunes67
Copy link

Hello, could someone help me build an .exe program with eel using electron on windows?

@ghost
Copy link

ghost commented Mar 24, 2021

I am still not getting how to use eel with electron please help

@ghost
Copy link

ghost commented Mar 24, 2021

Hello, could someone help me build an .exe program with eel using electron on windows?

I also want to do this

@amiotk
Copy link

amiotk commented Jul 14, 2021

I've managed to create a single exe for windows. Sadly, had to modify run() function of electron.py to make it work with pyinstaller --noconsole option. Therefore I pulled-in eel source code instead of installing eel package via pip. Below is a recipe how to make eel + electron into a single file executable.

Disclaimer
Modifying eel source code seems wrong and I'm not sure of all implications of this change but that was the only way I was able to make the exe work without python console.

Modifying the example

  1. Download eel source code and go to electron example
  2. Copy extracted electron release package into ./electron directory
  3. Copy eel sources into ./eel directory and modify electron.py:
def run(path, options, start_urls):
    cmd = [path] + options['cmdline_args']
    cmd += ['.', ';'.join(start_urls)]
    #sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE)
    sps.Popen(cmd, stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE)
  1. Modify hello.py file:
import eel
import os
import sys

# Set web files folder
eel.init('web')

@eel.expose                         # Expose this function to Javascript
def say_hello_py(x):
    print('Hello from %s' % x)

def resource_path(rel_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, rel_path)

eel.browsers.set_path('electron', resource_path('electron\electron.exe'))

say_hello_py('Python World!')
eel.say_hello_js('Python World!')   # Call a Javascript function

eel.start("index.html", mode="electron")
  1. Overall example project directory should look like this:
> 09 - Eelectron-quick-start
    > eel
    > electron
    > web
    - hello.py
    - main.js
    - package.json
  1. Run pyinstaller:
pyinstaller hello.py --hidden-import bottle_websocket --add-data eel;eel --add-data web;web --add-data electron;electron --add-data main.js;electron/resources/app --add-data package.json;electron/resources/app --onefile --noconsole --debug all
  1. Launch executable dist/hello.exe

@ChrisKnott
Copy link
Collaborator Author

@amiotk that's great!

After long hiatus due to life stuff I am looking to push some improvements to Eel and I will definitely be improving the packaging which is not great at the moment.

@mouadessalim
Copy link

Yeah if you can also do some improvement to work also with cx_freeze because you already now that pyinstaller is detected like a virus

@omebay
Copy link

omebay commented Apr 14, 2022

Hello,
I am trying to create an application with python in the backend and electron on the frontend. For communication, I am using python eel. However, when I try to run my python which should launch the electron, I get the following error.
Screen Shot 2022-04-15 at 2 11 25
This is my file structure

Those are my files:
run.py

import eel
eel.init('web')
eel.start('index.html', mode='electron', cmdline_args=['./node_modules/electron/dist/Electron.app', '.'], port=8080)

package.json

{
    "name": "my-electron-app",
    "version": "1.0.0",
    "description": "Hello World!",
    "main": "main.js",
    "scripts": {
        "start": "electron ."
      },
    "dependencies": {
        "electron": "^18.0.3"
    }
}

main.js:

const {app, BrowserWindow} = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL('http://localhost:8080/index.html');
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  app.quit()
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

However, when I change the main.js to:

const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
    
  })

  win.loadFile('./web/index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

and run npm start on terminal everything works just fine. How could I use python eel to launch my electron app?

Thank you in advance.

@omebay
Copy link

omebay commented Apr 14, 2022

EDIT for the post above:

I've realized that when I try to run electron using python eel in the terminal, it starts the electron located in the opt/homebrew/....
however, when I use npm start, it uses the electron inside my project folder.

How can I specify python to use the electron located in my project folder?

@FareedFarag
Copy link

I've managed to create a single exe for windows. Sadly, had to modify run() function of electron.py to make it work with pyinstaller --noconsole option. Therefore I pulled-in eel source code instead of installing eel package via pip. Below is a recipe how to make eel + electron into a single file executable.

Disclaimer Modifying eel source code seems wrong and I'm not sure of all implications of this change but that was the only way I was able to make the exe work without python console.

Modifying the example

  1. Download eel source code and go to electron example
  2. Copy extracted electron release package into ./electron directory
  3. Copy eel sources into ./eel directory and modify electron.py:
def run(path, options, start_urls):
    cmd = [path] + options['cmdline_args']
    cmd += ['.', ';'.join(start_urls)]
    #sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE)
    sps.Popen(cmd, stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE)
  1. Modify hello.py file:
import eel
import os
import sys

# Set web files folder
eel.init('web')

@eel.expose                         # Expose this function to Javascript
def say_hello_py(x):
    print('Hello from %s' % x)

def resource_path(rel_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, rel_path)

eel.browsers.set_path('electron', resource_path('electron\electron.exe'))

say_hello_py('Python World!')
eel.say_hello_js('Python World!')   # Call a Javascript function

eel.start("index.html", mode="electron")
  1. Overall example project directory should look like this:
> 09 - Eelectron-quick-start
    > eel
    > electron
    > web
    - hello.py
    - main.js
    - package.json
  1. Run pyinstaller:
pyinstaller hello.py --hidden-import bottle_websocket --add-data eel;eel --add-data web;web --add-data electron;electron --add-data main.js;electron/resources/app --add-data package.json;electron/resources/app --onefile --noconsole --debug all
  1. Launch executable dist/hello.exe

Thanks for this. I've noticed that changing the stdout to PIPE couldn't let me do any console.logs() in main.js (electron app). I had some console logs on states like browser-window-blur or focus (for shortcut registering), and the app would just close with no errors (i have an event listener that closes the app on uncaught exceptions). I changed stderr back to sys.stderr to view the error and its a broken PIPE (EPIPE: pipe broken, write).

If I remove all my console logs on electron side, the exception didn't rise anymore. Thought I would share this for anyone potentially having the same issue and please let us know if you know a solution for this. Thanks.

@KarimMejri93
Copy link

Hello, I am trying to create an application with python in the backend and electron on the frontend. For communication, I am using python eel. However, when I try to run my python which should launch the electron, I get the following error. Screen Shot 2022-04-15 at 2 11 25 This is my file structure

Those are my files: run.py

import eel
eel.init('web')
eel.start('index.html', mode='electron', cmdline_args=['./node_modules/electron/dist/Electron.app', '.'], port=8080)

package.json

{
    "name": "my-electron-app",
    "version": "1.0.0",
    "description": "Hello World!",
    "main": "main.js",
    "scripts": {
        "start": "electron ."
      },
    "dependencies": {
        "electron": "^18.0.3"
    }
}

main.js:

const {app, BrowserWindow} = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL('http://localhost:8080/index.html');
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  app.quit()
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

However, when I change the main.js to:

const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
    
  })

  win.loadFile('./web/index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

and run npm start on terminal everything works just fine. How could I use python eel to launch my electron app?

Thank you in advance.

I Have the exact same problem. Did you manage to solve it? Thank you!

@TEJA1210
Copy link

Hii i think i have the solution for running eel in electron app.

Kindly check this repository there will be a example app included with description. Thank you

https://github.com/TEJA1210/Eel-Electron-App

@rchvalbo
Copy link

rchvalbo commented Feb 5, 2023

Hello, I am trying to create an application with python in the backend and electron on the frontend. For communication, I am using python eel. However, when I try to run my python which should launch the electron, I get the following error. Screen Shot 2022-04-15 at 2 11 25 This is my file structure
Those are my files: run.py

import eel
eel.init('web')
eel.start('index.html', mode='electron', cmdline_args=['./node_modules/electron/dist/Electron.app', '.'], port=8080)

package.json

{
    "name": "my-electron-app",
    "version": "1.0.0",
    "description": "Hello World!",
    "main": "main.js",
    "scripts": {
        "start": "electron ."
      },
    "dependencies": {
        "electron": "^18.0.3"
    }
}

main.js:

const {app, BrowserWindow} = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL('http://localhost:8080/index.html');
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  app.quit()
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

However, when I change the main.js to:

const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
    
  })

  win.loadFile('./web/index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

and run npm start on terminal everything works just fine. How could I use python eel to launch my electron app?
Thank you in advance.

I Have the exact same problem. Did you manage to solve it? Thank you!

Any solutions to this issue?

@Mister-SOSA
Copy link

Was anyone able to figure out this obstacle?

When I run this, it opens an electron browser but it is just blank. When I go to localhost:8000 on my browser, I can see my page but there is no CSS being applied to it...

I am also having a similar issue. My css file is being loaded by Electron, but the styles are not being applied. It's not due to the syntax itself or anything, because the page loads fine with the appropriate styling if you just load it in the browser.

image

Very strange situation...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests