Skip to content

Webview bootstrap. Where to start? [$100] #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bobatsar opened this issue Apr 7, 2016 · 16 comments · Fixed by #728
Closed

Webview bootstrap. Where to start? [$100] #700

bobatsar opened this issue Apr 7, 2016 · 16 comments · Fixed by #728
Labels

Comments

@bobatsar
Copy link
Contributor

bobatsar commented Apr 7, 2016

I would like to have a python4android application which just starts a simple webview and accesses a local python webframework, like flask.

What is the recommended way to start.
Should I take the sdl2 bootstrap and generate a new stripped down version or should I start with the empty bootstrap?

Is there anything already done in that direction?

How can the webview be started? Should it be initialized by a java class or is it ok to use pyjnius like in the following gist?
https://gist.github.com/tito/6118172

Did you help close this issue? Go claim the $100 bounty on Bountysource.

@inclement
Copy link
Member

I'd take the SDL2 bootstrap as a reference for the python unpacking etc, but strip out the SDL2 stuff and replace it with a webview. I'm not sure how threading will need to work with the python interpreter (maybe it will already), so I might start by running flask in a service.

Is there anything already done in that direction?

I'll probably try this sometime if nobody else does first.

@kollivier
Copy link
Contributor

Actually, I've been making a project that is a wrapper above Kivy, wxPython, etc. specifically for the purpose of creating cross-platform apps that use Web UI. You can check it out here:

http://pyeverywhere.org/

Follow the Getting Started section and let me know if you see any issues. Even if it doesn't work for you, the pew/kivy_pew/webview.py module can show you a start on how to do it yourself.

@bobatsar
Copy link
Contributor Author

I think Pyeverywhere is already too much for our purpose. But thanks for the link. Perhaps it can help in the future.

I had a look to strip out the sdl stuff but I really don't know whats still needed in the java classes and so on.

@inclement, could you try that?
I think the webview activity should be started from Java and the main python thread should be available for anything else (like the webframework)

The only thing which is needed as a connection between the two components is to provide a URL for the Webview to display.

thanks a lot

@bobatsar bobatsar reopened this Apr 13, 2016
@kollivier
Copy link
Contributor

Sorry, I wasn't suggesting just pulling the whole project into Kivy. Feel free to just integrate the code I wrote to get the Android WebView loaded into Kivy if it works for your purposes. It's mostly just some pyjnius code. There shouldn't be any licensing issues between the projects but if there are, let me know and I'll give permission to license it under Kivy's license. The main part of it is here:

https://github.com/kollivier/pyeverywhere/blob/master/src/pew/kivy_pew/webview.py

PEWWebViewClientInterface does rely on some Java code for callbacks, but you can either just delete that part or incorporate the Java code too (ideally the latter if I'm to use this on my project :). Having Kivy just start a WebView and not have to load SDL is what I want as well so I'd be more than happy to remove this code from my project if Kivy worked that way instead. I'm somewhat new to Kivy so I am just not at the point of knowing how to make that happen.

Also, I'm working on an iOS version of this approach as well that uses pyobjus and WKWebView (which unfortunately runs afowl of pyojbus #124 so it won't be ready until that is tackled), and honestly I'd love to see a Kivy solution for this that works for both desktop (maybe using pyCEF / pyobjc) and mobile platforms.

@inclement
Copy link
Member

Surely you don't want to use kivy at all if you're just displaying a webview?

@kollivier
Copy link
Contributor

What I need is a Python toolkit that lets me build apps for mobile that use a Web UI but lets me run various Python modules for data analysis and machine learning. While I don't currently need Kivy's UI controls, the fact that it lets me build mobile apps out of the box, load and use native UI controls via Python wrappers, and integrate popular Python libraries is a real time-saver for me.

I know I could go lower-level with P4A and other tools and "build my own" but I have a Nov. deadline for my project so I need to be able to spend as much time as possible building my awesome app rather than building an optimized custom mobile Python dev toolchain that lets me build that awesome app. :)

I'm open to other solutions, of course, but in my experimenting with what's out there this has given me the best results so far.

@tito tito changed the title Webview bootstrap. Where to start? Webview bootstrap. Where to start? [$100] Apr 14, 2016
@tito tito added the bounty label Apr 14, 2016
@michaelrinderle
Copy link

michaelrinderle commented Apr 25, 2016

check out this repository

its agnostic but i run my apps off a multi processed bottle framework using a thin event layer between javascript and python. webviews are handled by bottle. sl4a is integrated so you have ability to call to the native android api as well. this approach works very well for me and i have an app currently used by a company that is quite happy with the performance so far after 6 months in production...

@inclement
Copy link
Member

This branch contains a basic, working webview bootstrap that displays a webview without including Kivy or SDL2. I intend to tidy it up and do a PR within the next few days, but it shouldn't be anything very complicated. I'll also try to make pyjnius work with it.

@bobatsar
Copy link
Contributor Author

bobatsar commented May 6, 2016

Hey @inclement, your branch looks promising.
I am trying to get your test flask example running but I still have some issues

It seems that the webview_includes folder is not created when the dist is built. I get the following error when trying to build an apk.

File "/root/.local/share/python-for-android/dists/dist_webview/build.py", line 98, in listfiles
    for item in os.listdir(d):
OSError: [Errno 2] No such file or directory: '/root/.local/share/python-for-android/dists/dist_webview/webview_includes'

If I then create the folder manually, I can build apks afterwards.

After building your testapps/testapp_flask/ apk I get the following error in adb logcat:

"..jnius.so" is too small to be an ELF executable: only found 0 bytes

I/python  (15060): imported flask etc
I/python  (15060): importing pyjnius
I/python  (15060): Traceback (most recent call last):
I/python  (15060):   File "main.py", line 37, in <module>
I/python  (15060):     from jnius import autoclass
I/python  (15060):   File "/data/data/xxx/files/lib/python2.7/site-packages/jnius/__init__.py", line 12, in <module>
I/python  (15060):     from .jnius import *
I/python  (15060): ImportError: dlopen failed: "/data/data/xxx/files/lib/python2.7/site-packages/jnius/jnius.so" is too small to be an ELF executable: only found 0 bytes
I/python  (15060): Python for android ended.

What could be the reason for this? Is this the same as in #234?

@inclement
Copy link
Member

inclement commented May 6, 2016

Looks like the webview_includes one is just me forgetting to check something into git. I'm not sure what's going on with pyjnius but it could be related (I made some unfinished changes), I'll look at it at the weekend.

The main thing left to do right now is have java load the flask site once the server is running, which is giving me some weird problems but can't be that hard.

@bobatsar
Copy link
Contributor Author

bobatsar commented May 6, 2016

The main thing left to do right now is have java load the flask site once the server is running, which is giving me some weird problems but can't be that hard.

Could that perhaps be done from python via jnius to get the webview and then load a URL. I can't just test this as I still can't get pyjnius to build (I tried with different sdk and ndk versions).

It is a little bit strange, now I did a clean_all don't get the webview_includes error. (I tried in two different environments), but the jnius error is still there. On the other pc it is still a problem.

I am currently building the app with the following commands, is that correct?

p4a clean_all
p4a create --dist_name dist_webview --bootstrap=webview --requirements=webviewjni,flask,pyjnius --android_api=21 --arch=armeabi-v7a
p4a apk --private testapps/testapp_flask/ --requirements=webviewjni,flask,pyjnius --package=com.flask.test --name="FlaskTest" --version=0.1 --permission=INTERNET --android_api=21 --arch=armeabi-v7a

When I build commit b4fac4f I can run the apk, but it crashes after some clicks. When I connect with Chrome to localhost:5000 it does not crash.

I/python  (18408):     HTTPServer.serve_forever(self)
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 227, in serve_forever
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 286, in _handle_request_noblock
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 310, in process_request
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 323, in finish_request
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 641, in __init__
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/SocketServer.py", line 694, in finish
I/python  (18408):   File "xxx/.local/share/python-for-android/build/other_builds/python2/armeabi/python2/python-install/lib/python2.7/socket.py", line 303, in flush
I/python  (18408): socket.error: [Errno 32] Broken pipe
I/python  (18408): Python for android ended.

This could be related to http://stackoverflow.com/questions/12591760/flask-broken-pipe-with-requests but the strange thing is it seems to work with chrome.

@inclement
Copy link
Member

inclement commented May 6, 2016

Could that perhaps be done from python via jnius to get the webview and then load a URL

Yes (although due to threading it probably needs a callback to the main thread), but it would need the user to insert this into their python code and I'd like to avoid this since it shouldn't be hard to just make java do it.

When I build commit b4fac4f I can run the apk, but it crashes after some clicks.

This seems to be a problem with flask master, but I have no idea what's going on. In my up to date branch (I'm not sure if I pushed it all here) I'm using the most recent flask release, which apparently doesn't have this problem.

Your build command looks fine, though I haven't tried that arch or such a new android api.

I'll be looking at this again in the next couple of days, and once I fix the java threading problem I'll make sure I can build using the clean branch as pushed here. That should hopefully fix it for everyone and then it can be merged.

@inclement
Copy link
Member

I've now fixed the problems I had and pushed everything to the branch, so it should probably work now. There was some unpushed stuff relating to pyjnius which would explain your problem there.

@inclement
Copy link
Member

Everything seemed to work in a clean p4a environment, so I've merged the webview bootstrap. Testing and new additions welcome!

@bobatsar
Copy link
Contributor Author

bobatsar commented May 9, 2016

I just tested your pushed changes and they look good.

The flask Broken Pipe exception still occurs sometimes but the updated flask recovers automatically after the error. So it seems the reason for the error is still there but flask can handle it now.

I just pushed some changes to a537d74 to make it possible to load a URL from java and python code.

@bobatsar
Copy link
Contributor Author

Hey @inclement, the webview_dist problem still exists. Even in the IRC someone else had this problem right now. Do you know, how to make sure the folder is created correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants