In a previous post, we explore Eel, which is a cool python library that allow us to create web based GUI's for our python scripts.
A simple application was created, using the minimal arguments for eel.start; in this post we're going to explore what other options we have in order to better fit our requirements/needs.
mode
mode will allow us to select which browser should be used to render our html files; default is set to chrome browser. Another options that can be used are: 'edge' for edge browser, 'electron' and 'chrome-app' for firefox.
host
host is used to specify the hostname for the underlying libraries that eel makes use; default is localhost.
port
port is one of the most useful options, sometimes the application won't start because default port is being used by other application, this should be fixed by specifying another port. Default value is 8000.
block
By default, when python interpreter reach eel.start line, application will be blocked, thus, remaining code won't be executed. If you want to run eel and not get stuck at that line, you can set block value to False.
size
size allow us to set the window size, it must be specified as a tuple containing width and height.
position
position allow us to specify the origin coordinates (top-left corner) of the window, it is also a tuple, but contains x-axis (horizontal) and y-axis (vertical). Remember, 0,0 is top-left corner.
geometry
Instead of using size and position option, you can pass a dictionary to geometry option. This dictionary requires keys size and position, and values for both are tuples. E.g.:
geometry={'size': (700, 400), 'position': (50, 50)}
disable-cache
Default is false, specifying True value will prevent the browser to cached our website.
close_callback
Allow us to modify what python should do when closing the window. By default, python will exit upon closing the window, which could lead to data loss if we closed accidentally, so we can modify default behavior to properly closed running tasks.
cmdline_args
We can add extra flags in order to start the browser, for example, running on incognito mode, disabling backgrounds or prompting a dialog before the browser starts. You can found useful flags here.
Example
Now that we have explore other options for eel, we'll update our python script from our previous post. HTML, CSS and JS files remains the same:
import eel
import random
from datetime import datetime
def close_callback(route, websockets):
if not websockets:
print('Bye!')
exit()
eel.init('web')
@eel.expose
def get_random_name():
eel.prompt_alerts('Random name')
@eel.expose
def get_random_number():
eel.prompt_alerts(random.randint(1, 100))
@eel.expose
def get_date():
eel.prompt_alerts(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
@eel.expose
def get_ip():
eel.prompt_alerts('127.0.0.1')
eel.start('index.html', mode='chrome',
host='localhost',
port=27000,
block=True,
size=(700, 480),
position=(0,0),
disable_cache=True,
close_callback=close_callback,
cmdline_args=['--browser-startup-dialog',
'--incognito', '--no-experiments'])
What is different now?
We just added a new function before eel.init, close_callback, this function allow us to modify default behavior upon closing the window; instead of only exit, it will first print "Bye!" in our python cosole. This is just a "dummy" test, but you can later update a file, update a database or simply perform a failsafe exit.
The other changes are simply the bunch of arguments we're passing to eel.start; we have added mode, block and localhost with its default values (there is no need to pass this arguments since we're using chome, True and localhost); with size we're determining the size of the browsers window, and with position, we specify that it must be start at the top-left corner. We're also changing default port, which is 8000 to 27000, the cache is also disable and for the close_callback argument, we specify our custom function.
Finally, for the cmdline_args we passed three arguments, first one will prompt an alert window which allows the user to know that the browser is starting and its PID. Second element, simply specifies to use an incognito window and third element prevent chrome from using experimental flags (like dark mode).
We have explored additional options for eel and updated our previous demo application with this options. In our next series we'll explore how to create an executable.
Top comments (0)