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

Closed

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>

Activity

  1. samuelhwilliams commented on Oct 21, 2019

    @samuelhwilliams
    Collaborator

    Sorry for the delay in responding to this. While this does expose Bottle as part of the public interface (whereas it's currently an implementation detail) I think this is a pretty valuable proposal.

    I'll add it for a v0.11.0 release and perhaps include it in a future v1.0.0-a2 release. This means that, should there be some negative consequences that I haven't thought of yet, it could be removed for an official v1.0.0 release - just FYI.

  2. added this to the v0.11.0 milestone on Oct 21, 2019
  3. added a commit that references this issue on Oct 21, 2019
    d053c1f
  4. RahulBadenkal commented on Jan 3, 2020

    @RahulBadenkal
    Author

    Hi, I was just trying to pass a beaker middleware (as shown in code above) but got the following error:

    Traceback (most recent call last):
    File "//run.py", line 52, in start_app
    eel.start(start_html_page, mode='chrome', port=0, app=app)
    File "//venv/lib/python3.6/site-packages/eel/init.py", line 161, in start
    run_lambda()
    File "//venv/lib/python3.6/site-packages/eel/init.py", line 151, in run_lambda
    app.route(path=route_path, callback=route_func, **route_kwargs)
    AttributeError: 'SessionMiddleware' object has no attribute 'route'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions