joeyespo / grip

Preview GitHub README.md files locally before committing them.
MIT License
6.47k stars 424 forks source link

API - Stop previewing markdown from Python #345

Closed Patitotective closed 2 years ago

Patitotective commented 2 years ago

I'm using grip to preview a markdown file in my application and I need to stop it from running. I have the below code inside a QThread and I'm not sure how to stop it.

self.app = grip.create_app(path="temp.md")
self.app.run(open_browser=True)

(Maybe browser_thread.stop()?)

Patitotective commented 2 years ago

I ended up doing this

class PreviewMarkdownInBrowser(QObject):
    def __init__(self, *args, **kwargs):
        super().__init__()

        self.args = args
        self.kwargs = kwargs
        self.app = None
        self.hyperlink = None
        self.server = None

    def run(self):
        self.app = grip.create_app(*self.args, **self.kwargs)

        self.hyperlink = f"{self.app.config['HOST']}:{self.app.config['PORT']}"

        self.server = multiprocessing.Process(target=lambda: self.app.run(open_browser=True))
        self.server.start()

    def stop(self):
        self.server.terminate()
        self.server.join()