Closed MikeTheWatchGuy closed 5 years ago
From @MisterY on how to run PySimpleGUIWeb on your Android device as outlined in Issue #128
Absolutely. I'm happy to do that and hope it helps others. These would be the rough steps:
- install Termux Android app.
- Run Termux console and execute
pkg install python
- run
termux-setup-storage
, as specified in the docs, to create links to common locations on your device.- run
pip install pysimpleguiweb
Now you have a working Python environment on your device. Now get a copy of your PySimpleGUI app/script. Using a web browser, you can download the file to the Downloads folder. Note that the Web version of PySimpleGUI is currently the only one that will work on Android. Kivy would be a possibility but the Remi web interface will also do.
- now go
cd storage
andcd downloads
to navigate to the Downloads folder,- run
python <your script.py>
to execute.This will result in no visible output in the console. Make sure that you specify the
web_port
parameter in your code for this reason. Using a random available port would make finding your app fairly difficult.
- open your web browser and navigate to
localhost:<your app port>
. That's it!It is also possible to run the app (web server) and the UI (web browser) from the same script, which would provide an appearance of a unified application. For reference see this. Remi web application (server-side) will close once the client triggers the Exit event. By adding Termux Widget add-on, one can create an application icon on Home screen that starts your app and open the GUI directly.
Thinking ahead, adding the PWA options, or just forcing a window without the address bar, would already provide a UI that resembles a native app.
What I need to do now is stitch together the right applications that will enable easily download of .py files to my Android device.
And then an easy way to (find and) launch them using termux.
It would then be possible, in theory, to create a GUI based LAUNCHER for the other GUI based Python programs you have! Bootstrapping at it's finest
What I would LOVE to be able to supply users is the SAME level of ease as I provide in the Cookbook and in the Readme. It must be TRIVIAL for people to be able to do this.
Can someone make recommendations on how to
Finally....
To download .py files to Android you could use wget
in termux, cURL
in termux, or use git
if you can get it installed to clone from a repo.
You could store the files in a Python Projects directory if you have root access or you could save them to a file saving app then navigate to their directory through termux.
You could run ssh commands from another computer to run the python script remotely.
Do you mind if I copy the instructions here, so that I can edit them, to provide more info?
Hope this helps others to come up with some new ideas. These are the rough steps to add GUI to your beloved applications on Android:
termux-setup-storage
, as specified in the docs, to create links to common locations on your device.apt update
and apt upgrade
to update your packages.pkg install python
pip install pysimpleguiweb
Now you have a working Python environment on your device. Get a copy of your PySimpleGUI app/script onto the device. For example, using a web browser, you can download the file from GitLab or GitHub to the Downloads folder. Note that the Web version of PySimpleGUI is currently the only one that will work on Android. Kivy would be a possibility but the Remi web interface will also do.
cd storage
and cd downloads
to navigate to the Downloads folder;python <your script.py>
to execute.This will result in no visible output in the console. Make sure that you specify the web_port
parameter in your code, in the Window() constructor, for this reason. Using a random available port, which is the default, would make finding your app fairly difficult from a browser.
localhost:<your app port>
. That's it!It is also possible to run the app (web server) and the UI (web browser) from the same script, which would provide an appearance of a unified application. For reference see this. Remi web application (server-side) will close once the client triggers the Exit event. This covers the "cleanup" phase. When I was using Flask, I had to come back to Termux to shut down the web app separately. By installing "Termux Widget" add-on, one can create an application icon on Home screen that starts your app and opens the GUI directly.
Thinking ahead, adding the PWA options, or just forcing a window without the address bar, would already provide a UI that resembles a native app. Hopefully Remi gets a bit more sophisticated in terms of the look and feel, which would provide a fantastic way to move ahead with real cross-platform applications.
* How can the .py file be launched other than termux?
Use Termux Widget. I think I was using this script, which just ran the app bootstrap. The bootstrap, though, would run the (web) app, wait until it can connect to it on the given port, and then run the web browser which connects to your web app on localhost. I agree that it should be even simpler to get all this going but it is a good start, overall.
* Download .py files, remotely, to an Android device. Is there a recommended FTP type thing?
I had published my app(s) on PyPi, allowing for two simple commands in Termux console to get it all up and running:
pip install gnucash-portfolio-webui
runapp.sh
or something like that. The benefit of this is that it installs all the dependencies specified in the setup.py. Then I also experimented with Termux Widget, as explained above, resulting in a Home app icon that opens up the web GUI with a Flask server running in the background. In the end, everything is available at a tap on the app icon.
Option 2 would simply be using Git to clone a sample repo, which would hopefully be available separately to the library. PySimpleGUIWeb, in that case, needs either to be in that sample repository, or pre-installed via pip somehow.
A sample PySimpleGUI file that's been successfully run on Android as a starting point
Everything here has been run on Android successfully. We should, however, try the full widget demo of all the available controls in the web version. Both Chrome and Firefox are available on Android, and pywebclient uses WebKit, so pretty much the functionality available rivals that of the desktop browsers.
OK, that's where I'm at right now. Hopefully some of you have more time to play with these and come up with fresh ideas. Perhaps there are other technologies out there and sharing the knowledge might be really helpful in that regard.
As it seems that pywebview does not work on Android, I am not 100% sure how I was opening the web browser (as I don't have that device available). The widget was linked to a .sh script, meaning the script was triggering the browser to open after starting the python web application in Flask. Some hints below.
Termux:Widget can be linked to a shell script (.sh).
Termux can handle URLs: https://wiki.termux.com/wiki/Intents_and_Hooks
Starting a web browser via Intent:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Termux has termux-open
and termux-open-url
commands, which can be called in a shell script to open up a URL in the default browser. This works well.
Both the server and the client can be run with
prog1 &
prog2 &
I got as far as running python and launching my main() program, but Remi didn't open a browser for me :-( I think this is where you started the other day. I suppose I need to specify a port number in my apps. I saw in your hello world that your Window call looks like this:
window = sg.Window("1st!",
web_debug=True,
#web_start_browser=False
web_port=8080
).Layout(layout)
I'll assume I need similar settings.
I'm taking the approach of using FTP to push across my .py files I want to run... .or that's the plan as I'm now loading an FTP APP on my tablet along with the Termux Widget that I bought.
Correct. That's what I mean with specifying the port number. Or, since I would not expect the web server to be running on an Android device, just use the port 80. That will allow connecting to just "localhost" instead of "localhost:port". Then see the message above in relation to how to get both running from the same script. Termux Widget is also available on F-Droid as it is Open Source but I also bought all the components a while back to support the devs for such a fantastic tool.
Running termux-open-url
could be used to download the files, also. Check it out.
Wow! I cannot believe this!
I can:
python my_code.py
AND up came my Window!!!
This was the code that just ran!
import PySimpleGUIWeb as g
layout = [[g.T('This is my first window')],
[g.In()],
[g.T('', key='_OUT_')],
[g.OK(), g.Cancel()]]
win = g.Window('My first Android Window', web_port=8080).Layout(layout)
while True:
e,v = win.Read()
print(e,v)
if e in (None, 'Cancel'):
break
win.Element('_OUT_').Update(v[0])
win.Close()
I've edited my instructions heavily, to add more details.
You can create a shell script in your home folder, that takes you to wherever you want. ;) Or you can memorize your home folder location in Total Commander, for easy file transfer.
Congratulations! :)
There HAS to be a BIG shout out that goes to @dddomodossola and Remi for this entire achievement, for it is because of Remi that PySimpleGUIWeb is able to do this. Of course it's actually Remi that's doing all the work and is the real achiever here.
I'm just disguising the hard work of the Remi team as my own by putting other clothing on it. Same Remi, different shirt and tie.
PySimpleGUI is much more than this, is a standard API for different gui frameworks, is a well documented simple interface. And you @MikeTheWatchGuy , you are working really well, giving help to all users. Have a good coding ;-)
@MikeTheWatchGuy @MisterY of course you can also access mobile device sensors. Here is a remi example https://repl.it/@DavideRosa/devicesensors . It should be simple for you to implement this in PySimpleGuiWeb .
Here is also an example to get smartphone photos https://repl.it/@DavideRosa/smartphoneremotephoto
@dddomodossola See what I mean about taking credit for your hard work? You've just supplied me with some awesome code, that you developed, that I will be able to pull into some PySimpleGUI programs. You make me look great!
I can't thank you enough!
What I'm working on today is more officially documenting this process as well as locating more techniques and tools to load and run code more easily.
I'm finding the FTP part to be trivial to do. I'm trying to find ways to get away from having to type into Termux.
Any clue how to get console output going to Termux? It sure would help with debugging. I'm still trying to determine how to route stdout out a multiline Remi Widget so I can implement my Output Element.
Tonight or tomorrow I will be able to work on my laptop and I will provide you a solution for stdout redirection. ;-)
I'm happy there are more minds dealing with this now and hope something useful comes out of it. @dddomodossola, I've posted a few thoughts/questions on reddit. Just thinking what the optimal use of time/effort would be for these technologies. @MikeTheWatchGuy is right about the need for the whole process to be easy and straightforward. I've tried to create a couple of forms for a simple report in both PySimpleGUI and Remi but find it needlessly complicated. Or, rather, there is a need to learn yet another bridging technology. Somehow, I'd prefer to use widely-supported standards and keep the extra code to a minimum. I.e. developing the layouts in HTML (which is neat when using Flask and some templating engine, for example), and the functions in Python. Some food for thought. Nice weekend!
Oh, don't get me wrong about the current ease.
I was able to follow the directions to use termux and was up and running in under 20 minutes.
I have spent DAYS previously trying to get ANYTHING to run PySimpleGUI code, of any kind. It was difficult.
I recall getting something to run at one point, but I gave up as I'm sure it was still too difficult and of limited use.
I'm willing to go with Termux as the first set of instructions. It's a single program to install. Just wanting to make it even easier so I'm exploring the different options and apps.
I FINALLY managed to get SSH to run from my PC's dos shell, connecting over to my Android Tablet!
However, I was quickly disappointed to find that Python isn't on the path or there's some other reason my SSH isn't finding it.
This is the PATH setting after I SSH login:
PATH=/data/data/berserker.android.apps.sshdroid/dropbear:/data/data/berserker.android.apps.sshdroid/home/.bin:/usr/bin:/usr/sbin:/bin:/sbin:/system/sbin:/system/bin:/system/xbin:/system/xbin/bb:/data/local/bin
These are the steps / problems that I want to solve:
1, 2 are one-time things 3-5 are the repetitive ones done for development
Ideally, the User Code would be represented by an "App Icon" on the Android desktop, somehow. This icon will launch the Python code. It would be GREAT to have an option where this icon also launches Chrome or other browser and automatically connects to the user application.
The experience would then be one of - user taps app icon, user interface is shown to user to begin interacting with.
This seems like a reasonable set of goals. I'm trying various tools and options. It's got a ways to go before I can announce anything anywhere (damnit, this is exciting!). Want to make it as trivial as the cookbook is for everything else. It's quite possible to make this process very easy it would appear.
I've been thinking about what I would want to use my tablet or phone for with Python and PySimpleGUIWeb.
One that has been standing out is the YOLO Demo Application. Of course that means getting the YOLO demo first to run on PySimpleGUIWeb. And it also means figuring out how to hook OpenCV up to the phone's webcam. This would actually be a useful application when running on a phone because it's portable.
I have an Image Element up and running on Remi, but I haven't seen what it'll take to hook up OpenCV to that implementation of Image.
This kind of app would really grab attention!! Running AI on your phone, with a GUI. Damn.
Pretty much all the steps listed can be done with a shell script. Termux Widget allows app icons linked to the scripts in .shortcuts directory. The question is if the home icons can also be created automatically. I'm sure there has to be an intent for that. That's how I was starting the browser before, by sending a web browsing intent. An Intent was useful in that case as people could have different browsers available. I might know more on Monday, when I get to my old device, which has a startup script.
Edit: Also, once Python is installed by the shell script, the execution can continue with a Python script, if it offers more functionality.
There is a Termux:API package, but I don't see an option there to create a shortcut. Perhaps it is as simple as sending an intent: https://stackoverflow.com/questions/6424246/creating-shortcuts-in-android-via-intent
Here is a quick sample on how to send an intent in console directly: https://github.com/termux/termux-app/issues/83
Termux:Task also allows triggering Termux tasks automatically. This will start a session if one is not running.
@MikeTheWatchGuy here is the promised code for stdout redirection https://github.com/dddomodossola/remi/issues/283
@MisterY I will reply to your questions soon and I will code an example for widgets hot-reload
Mike, when working in Termux, you can use mc, if you like that kind of interface. Makes it easy to create folders, copy stuff, and navigate around.
Also, use the special keys (tab, up and down arrows) at the bottom of the console when typing commands.
@MisterY I replied to your questions on reddit ;-)
@MikeTheWatchGuy It could be difficult to install OpenCV on Android, since it is a compiled library. However, one of the examples I sent you previously allows to grab photos from a smartphone remotely, and so you can use the smartphone as a mobile camera. The PySimpleGUIWeb/opencv capable application will be running on a standard PC.
Oh, right! OK, OpenCV was asking a lot for the first program!! I'm just elated it's running on the tablet, still, at this point and that it was easy to do.
Oh, I haven't been able to get the Termux Widgets running on my FireTablet (I have hacked it so it runs google play). I was able to get the floating Termux working however.
Seems like Termux may hold all of the answers to the stuff I'm trying to do. Now that I know a bit more about my login info, I will try their ssh again, perhaps look at their ftp too.
@MikeTheWatchGuy have you seen the stdout redirection solution ? https://github.com/dddomodossola/remi/issues/283
Yes!! Saw it earlier but only now had time to comment on it. Thank you SO much for getting to it and writing the code for me! You make this really easy!
I have implemented the Output Element and now stdout can go straight to the window.
I'm now wondering if it'll make running on Android much easier since the stdout is not going to the termux console for some reason. Perhaps we can debug using a portion of the window?
I've managed to retrieve the old startup script. As I have suspected, it is fairly simple and utilizes the elements mentioned above:
# Run the server
# python ~/server/app.py &
gpweb &
# Run the browser
am start --user 0 -a android.intent.action.VIEW -d "http://localhost:5000" &
wait
echo Complete!
Note that gpweb in this case represents a CLI interface for the gnucash_portfolio_webui package. The content of the script are available there. I guess it just starts the Flask web server, nothing special.
This code went into gpweb.sh, which was placed in .shortcuts, and available for Termux:Widget shortcuts on the home screen.
EPIC
!Everyone on the planet has a phone, and most of them are android phones.
We're about to give Python programmers the ability to show their friends their code by handing them their phone. A key part of all this is interactivity. By launching right into a GUI, the user has an easy to use interface versus trying to do anything command line (on a phone).
Clearly there has been a lot of capabilities or else we wouldn't have gotten this far. But they've not been pulled together into some interesting solution. It'snot like we're the first people to run Python on an Android. But, I would like for us to the the first to make it well-documented, SIMPLE, with a very high success rate.
Programmers like to show people their stuff and this opens countless ways. I accidentally connected to my phone today from my PC. You could let your friends run your Python code that's running on your phone but controlled through Chrome running on your friend's phone.
If we make this a trivial, easy for anyone to do, thing, it could really get popular quick! There are not many ways for people to "deploy" a Python program on a phone today. I know because I researched it when I started a Kivy port.
I want to document both a "local" solution and a remote one. The local one the browser is run locally (you want your friend your phone). The remote one you send a link to your friend to your app that's running on your phone and they connect to it via a browser on their phone.
I didn't use remi and don't know much about it. Infact, I'm not experienced in coding or such things.
I might be wrong, but you can create a script that will create a manifest file , add a script to the Homepage html and some stuff like that which will prompt the user to add the web app (link of the web app with an icon) to Home Screen. :-) But it requires HTTPS also. :-(
You can check it here: https://developers.google.com/web/fundamentals/app-install-banners/
I am working now on ONLY using what's on windows and the standard Termux program. That includes using the windows based FTP client to move the code over.
I want to provide instructions, at least the first set of instructions, that uses tools everyone has on their system. It's also critical that the instructions be kept SIMPLE, easy to follow, brief, and most of all.... THEY HAVE TO WORK, the FIRST TIME.
I'm getting close.
Find your Android device's IP address by typing:
ifconfig
from within Termux. Look for wlan0 and then look for "inet addr:". This is your device's IP address that you'll need for accessing via windows
pytcpsvd -vE 0.0.0.0 1111 ftpd -w /sdcard/py &
The & is added at the end so that it'll run in the background.
Move code over from windows to your Android device on windows:
ftp
open 192.168.0.123 1111
send c:/full/path/to/mycode.py
At this point you've sent your code to your Android device and now you're ready to run it!
Again inside termux: cd /sdcard/py python mycode.py
Now your code is running!! You only need to "connect" a browser to it so see it run. On your tablet you can open it locally by launching Chrome and navigating to: 127.0.0.1:8080
OR, you can also connect to your program REMOTELY by pointing a browser to your tablet: 192.168.0.123:8080
What's REALLY fun is that you can run your GUI on your phone or on your PC! Or, your friends can connect to your program on your phone from their phone, or at least that's the hope.
It's CRAZY that you don't need to root your device to do this!
@MikeTheWatchGuy Great Work! You are working on this open-source project with passion and you are making a really good library ;-)
look at the Qpython android application. It could be another way to run PySimpleGuiweb. I was able to use it with remi some times ago.
@MS-Jahan, what you are describing is generally known as a Progressive Web Application (PWA).
@MikeTheWatchGuy, people can transfer the files via chat apps, like Telegram for example. It should also be possible to transfer a multi-file package in an Egg created with setuptools. However, I don't see much point in running the server on an Android device and connecting from another device. The opposite would be a more likely scenario. Web app will always be the same so it can be run anywhere, be it a PC, cloud instance, RPi... One thing that might also be helpful would be a reverse-proxy tool like ngrok, combined with a QR code. Also, you don't need to specify Chrome in the instructions. People can use a myriad different browsers available on Android, or just a plain WebView component, integrated in other app hosts.
@MisterY The connecting into your application via a friend's phone wasn't meant so much as a "practical application" as it was a super-easy way to show off your app for your friends. Remi is running a server to begin with, whether it's connected to from remote or local browsers isn't of any importance technically speaking.
I was connecting last night remotely from my PC to my tablet because of how much easier it was to test stuff. From my PC, I "pushed" across my code, started it from my tablet, and connected to it via the PC. The only operation I had to do from my tabled was running my Python program.
Although, the more "application-like" it can be made, the better the overall experience will be (having the browser tabs/controls disappear quickly for example.
I still need to look through all the posts at the help that's been provided on things like how to launch the app, etc. I'm the short-term, I may go ahead and re-type these instructions above into a README and post it! It sure seems to be a something I can duplicate over and over on the tablets and phone I have. I'm anxious to post it to Reddit to get the reaction from the community at large.
I thought this will be useful.
After reading @MisterY 's reply, I have remembered an alternative of ngrok for port forwarding without installing anything but a ssh client, just A SINGLE LINE OF COMMAND like this will do the rest:
ssh -R pyapp:80:127.0.0.1:8081 serveo.net
On Termux, just apt install openssh
to install a ssh client.
This will forward your device's 8081 port to pyapp.serveo.net (in port 80). One just have to send the link pyapp.serveo.net and the other person(s) will get that in real quick. And yeah, it is a free service. :-D
More details: https://serveo.net/
I tried working yesterday with Termux's openssh, but after spending over an hour on it, I was never able to get logged in successfully. There was all these key pair things that seemed to be worked out that I was never able to get worked through.
I just discovered something really interesting while working with my phone.
I am trying all of the different Android devices I can find in my house, which turned out to be a LOT of them. I just got around to using my newest device, my Samsung phone.
This time I attached to the GUI both locally to the phone and remotely from my PC. BOTH windows updated at the same time with the same information! This has me thinking that you and a friend can play a 2-player game by both interacting with the same screen.
Maybe one person has controls over on the left, the other on the right. Or maybe you take turns. I dunno, but it sure seems like a great setup for both showing off your Python code to a friend, but also maybe writing a game you two can play too!
@MisterY feel free to post instructions on how to "transfer the files via chat apps, like Telegram". I don't know how to do that so I documented the way I was able to figure out how to do it.... via FTP.
It's one thing to "transfer" a file to your phone. It's another thing to find it and run it as a Python file.
I dunno about anyone else, but my Python source files are on my PC, not my phone, not my tablet, and not in any apps on devices. It's either going to be a push from my PC to my phone or a pull from my PC from my phone. Regardless the 2 devices that are used here in my home are my PC and the Android device. So, it made perfect sense to me to run a server on the phone that I connect to from my PC.
I'm shooting for SIMPLE. SUPER SIMPLE.
For now I'm looking for no apps to install other them Termux, anywhere. Not on the PC, not on the Android device. If we want to get slicker / easier by installing more helper tools, great, but first I want to get it to work in a way that is the easiest for everyone to get to work the FIRST time they try it.
The more crap people have to do, the less likely they are to TRY it. I'm shooting for "hey, give it a try" at this point.
Just released PySimpleGUIWeb version 0.21.0 that will help with this Android effort as follows.
If you set disable_close=True
when creating the Window, then you can safely exit the browser tab or the entire browser without it generating a None
event. You can reconnect with the page in fact and it will work as if it was never disconnected to begin with.
If you do not set this parameter and you accidentally close the browser, then you will have to re-run the python
command again from Termux, a pain in the ass.
Just remember to make it possible for your users to close your program somehow by giving them an Exit button or something like that, assuming you want them to be able to exit the program.
Can you do a
pip install --upgrade PySimpleGUIWeb
from within Termux and it will get this new version of PySimpleGUIWeb on your Android device.
Here's a nice little timer program that will run until you click exit. droid_timer.txt
You can connect to it multiple times both locally on the device as well as remotely.
There are 2 important parameters to set in Window calls for Android versions of PySimpleGUIWeb programs:
web_port=8080
disable_close=True
Hello @MikeTheWatchGuy have you looked at this? https://github.com/dddomodossola/remi/issues/279
Perhaps a useful thing would be to use something like Automate and put all the necessary steps together in a script. I see that it supports FTP. It is compatible with Termux:Tasker plugin. If there is a better, Open Source alternative, I'd like to know about it.
I ran a quick test. It can start a Termux shortcut. Considering the shortcuts are shell scripts, the field of options is wide open.
We've had a significant kinda breakthrough this week with one user, @MisterY cracking the code and getting PySimpleGUI up and running on Android. I am going to post his instructions next as the next post....