Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Passing a bottle app instance to eel #199

@RahulBadenkal

Description

@RahulBadenkal

Is your feature request related to a problem? Please describe.
I am trying to get session management in eel. Since eel uses bottle, I found this link that uses beaker module to set up a session. But it requires an app instance to be passed to bottle.run() method (which eel internally calls) but as of now there is no way to pass that to eel

Describe the solution you'd like
Allowing eel.start() to accept a 'app' option by adding the following in int.py

eel init.py (Changes)

 btl_app = options.pop('app', None)
    def run_lambda():
        return btl.run(
            host=options['host'],
            port=options['port'],
            server=wbs.GeventWebSocketServer,
            quiet=True,
            app=btl_app)  # Passing the app instance from options

Full Code:
main.py

"""
Entry Point to The App
"""

import logging
import os
import sys
from tkinter import Tk, messagebox
import eel

import bottle
from bottle import request
import beaker.middleware
import time


def show_error(title, msg):
    root = Tk()
    root.withdraw()  # hide main window
    messagebox.showerror(title, msg)
    root.destroy()


@eel.expose
def increment_num():
    print("HERE")
    session = request.environ['beaker.session']
    session['num'] = session.get('num', 0) + 1
    print(session['num'])
    time.sleep(1)
    eel.call_py()()  # Calling caller (js function)


def start_app():
    # Start the server
    try:
        BASEDIR = os.path.dirname(os.path.abspath(__file__))
        web_dir = BASEDIR
        start_html_page = 'index.html'
        eel.init(web_dir)
        logging.info("App Started")

        session_opts = {
            'session.type': 'file',
            'session.data_dir': './session/',
            'session.auto': True,
        }

        app = beaker.middleware.SessionMiddleware(bottle.app(), session_opts)

        options = {'host': 'localhost', 'port': 0, 'app': app}  # Need to provide a btl app to eel

        eel.start(start_html_page, options=options)
    except Exception as e:
        err_msg = 'Could not launch a local server'
        logging.error('{}\n{}'.format(err_msg, e.args))
        show_error(title='Failed to initialise server', msg=err_msg)
        logging.info('Closing App')
        sys.exit()


if __name__ == "__main__":
    start_app()

Sample index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Session Test</title>
</head>
<script type = "text/javascript" src="eel.js"></script>

<script>
    eel.expose(call_py)
    function call_py(){
        eel.increment_num()()
    }
    </script>
<body>

<button onclick="call_py()">Click Here</button>
</body>
</html>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions