Webadmin

Webadmin is a simple web interface to configure and debug your application. You can customize basic behavour in default.conf. It is recommended to add these options in its own file webadmin.conf and reference this file in default.conf

Here is a basic example:

default.conf
 [config]
 webadmin_conf = config/webadmin.conf
webadmin.conf
 [webadmin]
 host = 0.0.0.0
 port = 8000
 users.admin.user = admin
 users.admin.password =
 users.admin.password_hash = pbkdf2:sha256:150000$$N2b3ky8d$$51fbf24e48d498bd5543d60a86bd94927fd4d6eb123bf2d81a7401666eeea5c0
 users.admin.roles = admin
 secret_key = 1b50383ec6945aff8993f018feb568fa
 on = 1
 home_on = 1
 config_on = 1
 installationrepo_on = 1
 tools_on = 1
 settings_on = 1
 sources_on = 1
 expressions_on = 1
 statistics_on = 1
 security_webadmin_on = 1
 security_certificates_on = 1
 ssl_certificate =
 ssl_certificate_key =
 ssl_on = 0
Webadmin
Section Key Default Description
webadmin Config Default Description
webadmin host 0.0.0.0 Webserver host address
webadmin port 8000 Webserver tcp port
webadmin users.admin.user admin Username
webadmin users.admin.password   Plain text password. If password_hash is set then this option is ignored.
webadmin users.admin.password_hash   Password hash generated with python -m netdef -ga command
webadmin users.admin.roles admin name of user role.
webadmin secret_key   Secret flask session key. Can be generated with python -m netdef -ga
webadmin on 1

Enable Webadmin.

  • 0 – disabled.
  • 1 – enabled.
webadmin home_on 1 Enable Webadmin‣Home.
webadmin config_on 1 Enable Webadmin‣Config.
webadmin tools_on 1 Enable Webadmin‣Tools.
webadmin installationrepo_on 1 Enable Webadmin‣Tools‣Upgrade.
webadmin security_webadmin_on 1 or 0

Enable Webadmin‣Tools‣Webadmin.

[config]
webadmin_conf=config/webadmin.conf

The default value is 1 if webadmin_conf exists in [config]

webadmin security_certificates_on 1 Enable Webadmin‣Tools‣Certificates.
webadmin settings_on 1 Enable Webadmin‣Settings.
webadmin sources_on 1 Enable Webadmin‣Sources.
webadmin expressions_on 1 Enable Webadmin‣Expressions.
webadmin statistics_on 1 Enable Webadmin‣Statistics.
webadmin ssl_certificate   File path to ssl certificate. Required if ssl_on=1.
webadmin ssl_certificate_key   File path to ssl certificate key. Required if ssl_on=1.
webadmin ssl_on 0 Enable https.
webadmin_views [viewident] 0

[viewident] is the unique name of a MyBaseView

  • 0 – disabled.
  • 1 – enabled.
Example
[webadmin_views]
Home = 1
webadmin_views Home 1 Enable Home view.
webadmin_views FileModel 1 Enable FileModel view.
webadmin_views SettingsModel 1 Enable SettingsModel view.
webadmin_views SourcesModel 1 Enable SourcesModel view.
webadmin_views ExpressionsView 1 Enable ExpressionsView view.
webadmin_views StatisticsModel 1 Enable StatisticsModel view.
webadmin_views Tools 1 Enable Tools view.

Override root endpoint

A common use case is to integrate an existing flask app into the root endpoint (/) of the webserver. The example shows how this is done by retrieving the webadmin WSGI app and register a new endpoint at ‘/’

first_app/main.py:

# function that register my custom flask app
def init_app(app):
    @app.route('/')
    def hello_world():
        return 'Hello, World!'
    return app

def main():
    ...

    engine = ThreadedWebGuiEngine.ThreadedWebGuiEngine(shared)

    # init my custom flask app as soon as the webgui engine is initialized.
    init_app(engine.get_flask_app())

    engine.add_controller_classes(controllers)
    engine.add_source_classes(sources)
    engine.add_rule_classes(rules)
    engine.load([__package__, 'netdef'])
    engine.init()
    engine.start()
    engine.block() # until ctrl-c or SIG_TERM
    engine.stop()
    ...

Override Webadmin‣Home

Copy the default html template.

netdef/Engines/templates/home.html:

{% extends 'home/home.html' %}

{% block home %}

        <p>Application version: {{version}}</p>

{% endblock home %}

Paste it into your application with extended information:

first_app/Engines/templates/home.html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{% extends 'home/home.html' %}

{% block home %}

        <p>{{app_name}} version: {{app_version}}</p>
        <p>netdef version: {{netdef_version}}</p>
        <p>Python version: {{py_version}}</p>
        <p>Platform version: {{sys_version}}</p>

{% endblock home %}

Now you only have to override the Home View by creating following file:

first_app/Engines/webadmin/Home.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys
import datetime
import platform
from flask import current_app
from flask_admin import expose

from netdef.Engines.webadmin import Views, Home

from netdef import __version__ as netdef_version
from ... import __version__ as app_version
from ... import __package__ as app_name

@Views.register("Home")
def setup(admin):
    Home.setup(admin, MyNewHome(name='Home', endpoint='home'))

class MyNewHome(Home.Home):
    @expose("/")
    def index(self):
        return self.render(
            'home.html',
            app_name=app_name,
            app_version=app_version,
            netdef_version=netdef_version,
            py_version=sys.version,
            sys_version=str(platform.version())
        )
  • At line 13 we replace the default Webadmin‣Home with your own
  • At line 17 we override the default Home class with our extended functionality

Override Webadmin‣Tools

Copy the default html template.

netdef/Engines/templates/tools.html:

{% extends 'tools/tools.html' %}

Paste it into your application with extended information:

first_app/Engines/templates/tools.html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{% extends 'tools/tools.html' %}
{% block system_panel %}
        <div class="panel panel-default">
            <div class="panel-heading">System</div>
            <div class="panel-body">
                <p>Uptime: {{sys_uptime}}</p>
                <div class="container">
                    <div class="row">
                        <a href="./cmd_dir/" class="btn btn-default col-md-2" role="button">
                            <span class="glyphicon glyphicon-list" aria-hidden="true"></span>
                            dir
                        </a>
                    </div>
                </div>
            </div>
        </div>
{% endblock system_panel %}

Now you only have to override the Tools View by creating following file:

first_app/Engines/webadmin/Tools.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from flask import stream_with_context, Response
from flask_admin import expose
from netdef.Engines.webadmin import Views, Tools

@Views.register("Tools")
def setup(admin):
    Tools.setup(admin, MoreTools(name='Tools', endpoint='tools'))

class MoreTools(Tools.Tools):
    @expose("/cmd_dir/")
    def hg_log(self):
        return Response(
            stream_with_context(
                Tools.stdout_from_terminal_as_generator(
                    "dir",
                    pre="Command:\n\n   hg log -r .:\n\nResult:\n\n",
                    post=""
                )
            )
        )
  • At line 5 we replace the default Webadmin‣Tools with your own
  • At line 9 we override the default Tools class with our extended functionality