Build a webinterface
#1
Question 
Hi all,

I am nearly new to the KODI Add-on development, but I wrote an Add-on in the last few days and it's working pretty good.
Now I want to add a webinterface to control the Add-on and/or give some input into the Add-on via webinterface (1. step) and via iOS-App (2. step).
I have something like the Hyperion-App for iOS in mind. Has someone realized this? Is there a easy possibility to do this in KODI Add-ons?

Thanks.

M4tRiX
Reply
#2
I suspect it's possible, but it's likely to be a fair amount of work.  Kodi has a general web interface, but I don't think there's anyway to tie add-on control into that.  So your add-on would have to have a service component that would activate a web/api server of some sort, listen on a specific port (preferably not any of the ones Kodi uses), and then have all the request/response logic built into it. You'd also be responsible for all the security for that too.  I think the YouTube add-on has a really basic web server in it that is activated when someone needs to enter their Google API keys, so you might look at that to see what they did.
Reply
#3
Since Python has plenty of tools for building web apps it's totally possible and I have some experience with it. First, you need a Python application server (a WSGI server). For simple scenarios Python's built-in wsgiref server will be enough. You need to make sure that your server correctly shuts down when Kodi exits. Second, you need a web application framework. There are plenty of them in Python but, again, for simple scenarios bottle is quite good and it is included in the Kodi addon repo as a library addon. One of advantages of bottle that it is self-contained and does not have any dependencies except for Python standard library. Here is an example of a simple Python web application based on bottle that can be implemented as a Kodi service addon: https://gist.github.com/romanvm/e1be0503...d1befbb020
Reply
#4
(2021-04-23, 09:38)Roman_V_M Wrote: Since Python has plenty of tools for building web apps it's totally possible and I have some experience with it. First, you need a Python application server (a WSGI server). For simple scenarios Python's built-in wsgiref server will be enough. You need to make sure that your server correctly shuts down when Kodi exits. Second, you need a web application framework. There are plenty of them in Python but, again, for simple scenarios bottle is quite good and it is included in the Kodi addon repo as a library addon. One of advantages of bottle that it is self-contained and does not have any dependencies except for Python standard library. Here is an example of a simple Python web application based on bottle that can be implemented as a Kodi service addon: https://gist.github.com/romanvm/e1be0503...d1befbb020

Hi Roman_V_M, thanks for your Feedback. I will check out „bottle“.
Quick questions because I couldn‘t find this in Google. How should I start? I have already a „Addon“, can I expand the Addon.xml with an extension point?
Do I have to install bottle in Kodi as an extra plugin?

Thank you!
Reply
#5
Maybe this will help https://github.com/tamland/kodi-flask-demo , if your using kodi 19, u would have to update it to python 3
Reply
#6
(2021-04-24, 09:17)Sidewinder_2011 Wrote: Maybe this will help https://github.com/tamland/kodi-flask-demo , if your using kodi 19, u would have to update it to python 3
Yeap this is one of the best ways to do it since it won't require yet another webserver/service running on the target device (which is really important for embedded/low powered devices). An add-on can have multiple extension points. So if your add-on define a main plugin extension point but also a web interface one (similar to this project) it will automatically be exposed to kodi's webserver even if the default webinterface is provided by other add-on (e.g. chorus2). The only drawback is the fact the user has to enable the webserver in Kodi settings.
Reply
#7
@M4tRiX You can add a service extension point to your addon.xml. And you need to add script.module.bottle as a dependency.

@enen92 Yes, I know that Kodi has a built-in WSGI server but I don't know if it is used in real projects so I'm not completely sure that it is implemented correctly (and adapted to Python 3/PEP-3333 for Matrix). wsgiref is a safe bet for simple scenarios.
Reply
#8
Hi everybody,

quick update on this.
So I was able to setup the extension point and the website will be displayed if I run "python app.py" manually via ssh. How can I start this on startup?

xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="test.pluign" name="Test" version="1.0.7" provider-name="Plugin">
    <requires>
        <import addon="xbmc.python" version="2.14.0"/>
        <import addon="xbmc.json" version="6.0.0"/>
        <import addon="xbmc.webinterface" version="1.0.0"/>
    </requires>
    <extension point="xbmc.webinterface" type="wsgi" library="app.py" entry="app" start="startup"/>
    <extension point="xbmc.python.pluginsource" library="addon.py">
        <provides>executable</provides>
    </extension>
    <extension point="xbmc.addon.metadata">
        <platform>linux</platform>
        <summary lang="en">Test Plugin Summary</summary>
        <description lang="en">Test Plugin</description>
        <license>GNU General Public License, v2</license>
        <language></language>
        <forum>nothing</forum>
        <source>nothing</source>
        <email>nothing</email>
    </extension>
</addon>

One more thing...
As you can see I have the addon.py in this file there some functions I want to call with the input of the app.py (flask webservice). But my ways are not working and I get error messages like "ImportError: No module named xbmc". XBMC is the first thing I am importing in the addon.py.

python:
from flask import Flask, request, render_template
from addon import TestFunction

app = Flask(__name__)

@app.route('/')
def hello_Lightymon():
    return 'Welcome to Lightymon!'

@app.route('/hello')
def my_form():
    return render_template('form.html')

@app.route('/hello', methods=['POST'])
def my_form_post():
    direction = request.form['direction']
    device = request.form['device']
    processed_text = device + " " + direction
    TestFunction(ldevice)
    return processed_text

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000, debug=False)

Extract from the addon.py
python:
import xbmc
import os
import sys
import xbmcaddon
import xbmcgui
import time
import subprocess
import urllib2

addon       = xbmcaddon.Addon()
addonname   = addon.getAddonInfo('name')
[...]

Any Ideas? Thank you very much.
Reply
#9
Quick update. Got the flask running on startup via autoexec (https://kodi.wiki/view/Autoexec_Service).

But I don‘t get it how I can interact with my addonSad
Reply
#10
As I've said above I'm not sure that Kodi's built-in WSGI server really works. The implementation as a service addon based on wsgiref server definitely works and I used it in one of my past project.
Reply
#11
(2021-05-01, 08:20)Roman_V_M Wrote: As I've said above I'm not sure that Kodi's built-in WSGI server really works. The implementation as a service addon based on wsgiref server definitely works and I used it in one of my past project.
Ok, so I changed the flask part to bottle...

Can you tell me how render html.files with bottle?
In flask I used
python:

app = Flask(__name__)
@app.route('/')
def hello_Lightymon():
return 'Welcome to Lightymon!'

@app.route('/hello')
def my_form():
return render_template('form.html')

@app.route('/hello', methods=['POST'])
def my_form_post():
direction = request.form['direction']
device = request.form['device']
processed_text = device + " " + direction TestFunction(ldevice)
return processed_text

if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=False)

So how is the syntax in bottle for "render_templates" and "request.form" to get the information of this form?
Thanks!
Reply
#12
(2021-05-01, 13:54)M4tRiX Wrote: Ok, so I changed the flask part to bottle...

It doesn't really matter which application framework you are using, but Bottle is self-contained while Flask have several dependencies and I'm not sure if all of them are available as Kodi library addons.
 
(2021-05-01, 13:54)M4tRiX Wrote: So how is the syntax in bottle for "render_templates" and "request.form" to get the information of this form?
Honestly, I don't remember. Bottle has quite extensive documentation and I'm sure you will find your answers there: https://bottlepy.org/docs/0.12/
Reply
#13
BTW, you can check my old project: https://github.com/romanvm/kodi.yatp/tre...ibs/server It had a web-UI based on Bottle and some JavaScript libraries.
Reply

Logout Mark Read Team Forum Stats Members Help
Build a webinterface0