0

I am new to python on android and I am trying to buid the small example of a web application found here ( http://pythoncentral.io/python-for-android-using-webviews-sl4a/ ) where a python script opens a web page and communicates with it (you enter a text in the web page and the python script process it and send it back to the web page for display). In my attempt to do this the python script can open the page but there is no dialog between the web page and the script. It seems that the line " var android = new Android() " in the web page doesn't work as in the example.

Here is the code for the webpage :

<!DOCTYPE HTML>
<html>
    <head>
        <script>
            var droid = new Android();

            function postInput(input) {
                if (event.keyCode == 13)
                    droid.eventPost('line', input)

                droid.registerCallback('stdout', function(e) {
                    document.getElementById('output').innerHTML = e.data;
                });
            }
        </script>   
    </head> 
    <body>
        <div id="banner">
            <h1>SL4A Webviews</h1>
            <h2>Example: Python Evaluator</h2>
        </div>

        <input id="userin" type="text" spellcheck="false"
               autofocus="autofocus" onkeyup="postInput(this.value)"
        />

        <div id="output"></div>

        <button id="killer" type="button"
                onclick="droid.eventPost('kill', '')"
        >QUIT</button>
    </body>
</html>

and the python code launching the web page above :

import sys, androidhelper
droid = androidhelper.Android()

def line_handler(line):
    ''' Evaluate user input and print the result into a webview.
    This function takes a line of user input and calls eval on
    it, posting the result to the webview as a stdout event.
    '''
    output = str(eval(line))
    droid.eventPost('stdout', output)

droid.webViewShow('file:///storage/emulated/0/com.hipipal.qpyplus/scripts/webview1.html')

while True:
    event = droid.eventWait().result

    if event['name'] == 'kill':
        sys.exit()
    elif event['name'] == 'line':
        line_handler(event['data'])

I really don't understand how the Android() instance in the web page was supposed to work. Thank you for any help ! (I am running qpython with SL4A library on android lollipop)

val
  • 1
  • 1

1 Answers1

0

Finally yesterday I found that qpython comes with a nice framework called Bottle. And it is very easy to use for a beginner like me.

from bottle import route, run, request, template

@route('/hello')
def hello():
    line = ""
    output = ""
    return template('/storage/emulated/0/com.hipipal.qpyplus/webviewbottle2.tpl', line=line, output=output)

@route('/submit', method='POST')
def submit():
    line = request.forms.get('line')
    output = str(eval(line))
    return template('/storage/emulated/0/com.hipipal.qpyplus/webviewbottle2.tpl', line=line, output=output)

run(host='localhost', port=8080, debug=True)    

# open a web browser with http://127.0.0.1:8080/hello to start
# enter 40 + 2 in the input and "submit" to get the answer in the output window.

and the template file :

<h1>bottle Webview</h1>
<h2>Example: Python Evaluator</h2>
<form action="/submit" method = "post">
    <input name="line" type = "text" value = {{line}}><br>
    <input name="output" type = "text" value = {{output}}>
    <button name="submit" type = "submit">submit</button>
</form>
val
  • 1
  • 1