Version 4 (modified by mvoncken, 13 years ago) (diff)




Webui Examples

Adding a page to the webui


Webui templates are dumb, they get passed data from python, and can't call uiclient api's directly.

They are there to format the data, not to execute logic.

Template language:

Flow of the data

Disclaimer, MVC is a highly abused therm, and i'm abusing it here.

deluged->xmlrpc->uiclient-> webui         -> data passed as arguments to template.
('Model'                 )  ('controller')   ('view') 

Example : a df page in webui

This example assumes you have deluge svn checked out to "~/src/deluge" And that your plugin-development-directory is "~/prj/deluge/plugins"

This plugin will add a page to deluge that display the output of "df -h" to a page in the webui.

Create a new plugin

cd ~/prj/deluge/plugins
python ~/src/deluge/deluge/scripts/ --name Df --basepath . --author-name "My Name" --author-email ""

Restart deluge(d) and enable the plugin in the gtk-ui.

  • Add a core method.

Add this method to ~/prj/deluge/plugins/Df/df/

    def export_get_df(self):
        "returns the result of 'df -h' as a string"
        import commands
        return commands.getoutput('df -h')

Test the new method.

create ~/prj/deluge/plugins/df/Df/ ; Contents:

from deluge.ui.client import sclient

print sclient.df_get_df() #export method is prefixed by plugin-name.

Execute test

cd ~/prj/deluge/plugins/Df/df
killall deluged & deluged  && python

Fix any errors until the test outputs the desired result.

Add the template


$def with (df_result)
<h1>df -h</h1>

Webui plugin

Edit ~/prj/deluge/plugins/Df/df/ and add this above ConfigForm?

template_dir = os.path.join(os.path.dirname(__file__),"template")

class df_page():
    #@api.deco.deluge_page #requires  login, see for more decorators,
    @api.deco.deluge_page_noauth #<-- this is easier for testing until i implement persistent sessions.
    def GET(self, args):
        df_result = sclient.df_get_df()
        return api.render.df(df_result) #push data to templates/df.html

class WebUI(object): #replace/remove the old WebUI class
    def __init__(self, plugin_api, plugin_name):
        log.debug("Df plugin initalized..")
        self.plugin = plugin_api

    def enable(self):
        api.render.register_template_path(template_dir) #<--added
        api.page_manager.register_page('/df', df_page)  #<--added

    def disable(self):
        api.render.unregister_template_path(template_dir) #<--added
        api.page_manager.unregister_page('/df')   #<--added

Test webui page

killall deluge & deluge -u web & visit http://localhost:8112/df


  • (template_dir/path) is a convention, it will be automagic somewhere in the future.