Josh English
Josh English

Reputation: 532

BottlePy and PyWebView

I am trying to build a small app in Bottle, and thought I'd try using pywebview as a viewer. When I run the following file, I get two instances of the webview window. The first one shows the page, the second shows a spinning wheel cursor. Closing the second window is shutting down the web server, I believe, but not killing the thread.

Why are there two windows showing up?

import sys
import threading

from bottle import Bottle, ServerAdapter
import webview


class MyWSGIRefServer(ServerAdapter):
    server = None

    def run(self, handler):
        from wsgiref.simple_server import make_server, WSGIRequestHandler
        if self.quiet:
            class QuietHandler(WSGIRequestHandler):
                def log_request(*args, **kw): pass
            self.options['handler_class'] = QuietHandler
        self.server = make_server(self.host, self.port, handler, **self.options)
        self.server.serve_forever()

    def stop(self):
        # self.server.server_close() <--- alternative but causes bad fd exception
        self.server.shutdown()

app = Bottle()
listen_addr = 'localhost'
listen_port = 8080

server = MyWSGIRefServer(host='localhost', port=8080)

@app.route('/')
def hello():
    return "Hello World!"




def start_server():
    app.run(server=server, reloader=True)


try:
    print(threading.enumerate())
    serverthread = threading.Thread(target=start_server)
    serverthread.daemon = True
    print("starting web server")
    serverthread.start()
    print("starting webview")
    webview.create_window('bottle test', "http://localhost:8080/")
    print("webview closed. closing server")

    sys.exit()
    server.stop()
except Exception as ex:
    print(ex)

Upvotes: 0

Views: 1179

Answers (1)

Josh English
Josh English

Reputation: 532

The issue was using reloader=True when running the server. Setting this to False prevents the second window from appearing.

Upvotes: 1

Related Questions