[Flask] Update GTK menu from Web Flask App

Juan Asensio Sánchez okelet at gmail.com
Thu Jun 23 17:57:57 EDT 2016


[I have posted this same question on stackoverflow,
http://stackoverflow.com/questions/37979079/update-gtk-menu-from-web-flask-app,
with no responses; i hope to be more lucky here]

Addition: I suppose this problem is something related to the app context,
but I can't imagine how to make this work.

Hi all

I am trying to create an Ubuntu indicator, that launches a Flask app in a
separate thread (using multiprocessing.Process), and from an action in the
Flask app, add an item to the indicator menu (in this simplified code, just
when the root page is loaded, a menu item with the current datetime should
be created). The problem is that although I get no errors, the menu is not
created. I am using GLib.idle_add as recommended in other places without
success. This is the code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import argparse
from datetime import datetime
from flask import Flask
from multiprocessing import Process
import signal

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, GLib, AppIndicator3


APP_ID = "test"
APP_ICON = "test"

class GtkFlaskIndicatorTest(object):

    def __init__(self, debug):

        self.indicator = AppIndicator3.Indicator.new(APP_ID, APP_ICON,
AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

        GLib.threads_init()

        self.app = GtkFlaskApp(self, debug)
        self.app.start_app()

        self.menu = None
        self.quit_menu_item = None
        self.build_menu()

        Gtk.main()

    def build_menu(self):

        self.menu = Gtk.Menu()

        self.quit_menu_item = Gtk.MenuItem(u"Quit")
        self.quit_menu_item.connect('activate', self.quit)
        self.menu.append(self.quit_menu_item)

        self.menu.show_all()
        self.indicator.set_menu(self.menu)

    def add_menu_item(self, text):
        print "add_menu_item: " + text
        GLib.idle_add(self.add_menu_item_cb, text)

    def add_menu_item_cb(self, text):
        print "add_menu_item_cb: " + text
        item = Gtk.MenuItem(text)
        self.menu.append(item)
        self.menu.show_all()

    def quit_control_c(self, signal, frame):
        self.quit(None)

    def quit(self, source):
        self.app.stop_app()
        Gtk.main_quit()


class GtkFlaskApp(object):

    def __init__(self, indicator, debug):
        self.indicator = indicator
        self.debug = debug
        self.app = Flask("GtkFlaskFlaskApp")
        self.app.add_url_rule('/', 'test', self.test)

    def test(self):
        text = str(datetime.now())
        self.indicator.add_menu_item(text)
        return text

    def start_app(self):
        self.app_server = Process(target=self.start_app_thread)
        self.app_server.start()

    def start_app_thread(self):
        self.app.run(port=8888, debug=self.debug, use_reloader=False)


if __name__ == "__main__":

    signal.signal(signal.SIGINT, signal.SIG_DFL)

    parser = argparse.ArgumentParser()
    parser.add_argument('--debug', action='store_true')
    args = parser.parse_args()

    GtkFlaskIndicatorTest(args.debug)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160623/8591f725/attachment.html>


More information about the Flask mailing list