Run python code right out of github on phone - android

Is there any mobile python IDE that let you get download code from GitHub directly into the app? In fact, IDE would be an overkill - I don't need to code in the app, just need to run code as a quick demo to discuss ideas with people. My code don't launch GUIs, only console outputs.
My phone is Android, but iPhone answers are also welcome. Thanks!
Update:
There are a few suggestions that this may be a duplicate with Is there a way to run Python on Android?. I did read that one before I asked. It is either not the solution I am looking for or I simply misunderstood it - Kivy seems to be a framework to let you develop mobile apps using python, but what I am looking for is something that I can download my github repository and let me run one python file. Or you can say I want a python interpreter on my phone that can execute code (multiple modules/packages) from a online repository.
Anyway, I tried Kivy, and I got a screen like this (not intend to go off topic like the other question did, just trying to show Kivy did not solve my issue):
PyDroid 3 is half way what I want. It is a good mobile IDE to write some simple code in. But I couldn't find a way to download a repository in there. Copy-pasting works for those all-in-one .py files, but not practical for those code living in multiple modules/packages.

I'd suggest installing Termux and using it to run the code just like a linux terminal

Related

python to android convertion with Kivy

I have a Python3 desktop application which I want to convert to an android apk. I saw that Kivy module exists and might be able to pull this off, but I am concerned about it's ability to make the apk work just like the python code. I use many different modules like PIL, opencv, pyserial, threading, watchdog, file_read_backwards etc).
Is this possible or I am asking for too much? And if it is, how can I change/handle for which android version it will the apk be?
threading is in the stdlib, so you have it
file_read_backwards is pure python and doesn't seem to require anything (as per setup.py) although the requirements_dev.txt lists a lot more things, so i'm not sure
PIL has a recipe (https://github.com/kivy/python-for-android/blob/master/pythonforandroid/recipes/pil/init.py), and there is also now a Pillow recipe, which is probably better to use (https://github.com/kivy/python-for-android/blob/master/pythonforandroid/recipes/Pillow/init.py) so that seems fine
pyserial is pure python, and i think i remember people having success with serial over usb on android, though i didn't try myself, it might require a device able to be an usb host and not just client, but i don't know much about that.
opencv has a recipe, it might be a more touchy one, as it hasn't been touched in years (aside from some cosmetic fixes), and i think i've seen people having issues with it, but i'd say it's worth a shot.
I'd say, it's certainly worth a shot, before converting any of your code, try just building a hello world application with kivy for android, then redo the build but adding your dependencies to the requirements one by one, and see if you can solve it or find help when it doesn't, if all go well, then look into porting your code, which is the part where the success will certainly depend more on you than on what kivy/python-for-android can do.

Can python build android apps?

I am learning python and i don't know that it will be helpful to me if i want to make android apps.I have read somewhere that python can make android app.But I want to ask you that is it a good option or we should always use andriod studio to make android apps because that is developed particularly for creating android apps.
I'm a core developer of Kivy, which has been mentioned in some of the other answers. I think it's the main option you have for making python apps for android, and it has both advantages and disadvantages.
The main technical disadvantages are (both in my opinion and I think the main problems I see people raise):
Startup speed: if the app isn't already running it takes a short time to spin up the python interpreter, up to a few seconds on older devices (or much longer if you code some things badly but that's avoidable), during which time a loading screen is displayed.
Lack of native look and feel: Kivy is its own opengl-based graphical framework, so it doesn't look like default android apps (it's customisable but still very hard to perfectly emulate something like that)
APK size: Because you have to package the python interpreter, the minimum apk size is about 7MB.
Ther are other potential disadvantages, such as the standard non-java problem of having to catch up the android api when it changes, but I consider these more minor issues, e.g. in this case since you can actually call much of the java api directly from python with pyjnius. I have a blog post about this. Another problem is that kivy's community is small compared to that of the java app community, though it's also quite active.
For some people, one or more of these are immediate or eventual barriers to using Kivy. For others they don't matter or are outweighed by the advantages, and for these reasons there are people using Kivy commercially on Android (and iOS). Personally I'm most interested in the wide space between 'I wrote a simple script' and 'I made a big polished android app', because I think it should be easier than it currently is to bundle simple things as simple apps, but that's just my own reason for using kivy.
If the question is,can i run python programs on android then by all means yes.
But if the intent is to create a mobile app usibg python then please look at kivy which is yet to support python 3.
But outside the love of python ,its best to stick to android studio for native android apps.
Chaquopy (https://chaquo.com/chaquopy/) is an option for Python on Android. It is a plugin for Android Studio so could include the best of both worlds - using the Android Studio interface and Gradle, with code in Python.
From the Chaquopy page:
With the Python API , you can write an app partly or entirely in Python. The complete Android API and user interface toolkit are directly at your disposal.
A in-depth review of Chaquopy is at http://blog.codelv.com/2018/02/a-look-at-chaquopy-python-sdk-for.html
The creator of Chaquopy also commented on that review with this:
I think there's room for many different approaches to Python on Android. Chaquopy focuses on giving complete access to all the features of the standard Android API and build tools. (For example, the XML layout file you mentioned was generated using Android Studio's WYSIWYG editor: it didn't have to be written by hand.) But if you want something more Pythonic, or portable to other platforms, then enaml-native or Kivy is the way to go.
CAVEAT: I have not (yet) tested this personally, so I cannot verify how well it works. It is rather complicated.
Additional: It appears to require a license key to run for more than 5 min and requires payment. If you decide to develop open source, there is an open source license which "will always be free of charge."
try kivy https://github.com/kivy/python-for-android
It can be used to create android apps.
Android doesn't come with a Python interpreter. So if you want to distribute an application written in Python, you will have to bundle a Python interpreter along with it. In other words, even a "Hello World!" app will be huge.
So yes, it's possible. But not recommended.
Yes you can using
1- Kivy library
2- install Ubuntu on a versatile machine
3- run buildozer to transfer your main.py file into APK file

kivy or something else for android UI

I am learning to program (python 3.x) and was wondering what would be best to learn for making android apps/games. I dont know anything about UI really but have heard that kivy? Is on android. Is that worth learning or should i use something else.
Thanks
Edit:
I am using sl4a and py34a on my android device.
Kivy is good, and will run on Android, but you can't make Android apps with it. It has no way of interacting with the Android API. You can run a kivy app on the device, but can not read sensors or anything like that. They have a project called pyjnius, which allows you to access Java classes from Python, but not with Python3, and it's not very easy to use unless you know Java.
You have SL4A installed, so you can use web technology to do UIs, including games, which can be rendered on the device using webviews. That said, Kivy is a much more mature and well maintained project.
I'm loving kivy. The irony is, that I found this topic here looking for help with python 3. Python 2 works perfectly, but 3 is still being made compatible. Other than that Kivy is great for beginner coders. Especially, because you don't have to code the complex stuff.

Android and Python

Can I port existing python scripts on android using SL4A or ASE ? What I specifically want to do is to create an android application with normal UI elements and run the python scripts from the application itself, get the output and display it. Is this possible ?
Personnaly I think that you ask implicitly for three points:
Compatibility Desktop/Mobile
The first is the compatibility between the python library on the phone and the python library in your computer. If you don't use third party library and be sure to include the extra python library provided by sl4a, you should generally be ok.
Packaging
The second point is about how to package a Python app for android. It can easily be done with a wrapper as well described on the sl4a website. In fact they distribute a copy of a chapter of a book about SL4A that describes how to do that. So that is possible, but bear in mind that the user will be required to install python if he hasn't done so already (this is a sort of alert at the launch of the app)
UI
The third thing that you should have in mind is that you are not a 100% free concerning the widget/layout you can use with sl4a. Namely you can't really do whatever you would be able to do in Java or Scala. So you might have to consider altering your UI and do a webview instead (that can communicate back and forth with the python) with a framework to obtain a "mobile look and feel"
Hope this helps.
I think, what you want to do is make an independently apk file, that includes the python interpret and your script code.
I don't know if this can be done in Android. The only whay I know is running your script from Android Scripting Environment.
If web interface is normal UI then look at this discussion http://groups.google.com/group/android-scripting/browse_thread/thread/f86812549d2686e2/f828f916411d7a95 . You can use Python, webView, HTML5 and JavaScript
I had a similar problem and finally solved it by writing a small singleton class in Java that runs the Python-4-Android binary from the SL4A installation in a subprocess created using java.lang.ProcessBuilder. So I'm not using the SL4A mechanisms at all (triggers, upcalls, etc), just borrowing Python.
This seems cleaner than trying to start and connect to a Python process through SL4A.
This is Python 2.7.1, cross-compiling extensions from Mac OS X Snow Leopard. My Python modules are doing only text input and output, accepting socket connections, etc. No interaction with the Android API. It all works fine: writing a Java stream to Python input and reading a Java stream to get Python output. C extensions are build using the P4A instructions . (Android could not find .so dynalibs until I added
[build_ext]
inplace=1
to setup.cfg during the build. I think this is because setup 'install' is never invoked on the Android. I'm just pushing with adb.
All the activity lifecycle states seem to be working, but I can't yet determine whether the sub-process is automatically suspended while the main process is in the stopped state.
I can post code in a couple of weeks if this is of interest. (Just departing on vacation.)
My plan for packaging is to put a ZIP archive of the Python code in /assets and have the app unzip during the first onCreate. I haven't implemented this yet, but I don't expect any problems.

A non-programmers guide to programming

Okay so I am a very low level programmer and happy to admit it. I can write simple programs things like VB and python etc given the basic syntax. I want to write simple programs for android, for instance a calculator that handles feet and inches and a program that simply searches a database of products and tells me the details of them. Is there a simple solution where I can drag and drop some buttons and text boxes and execute code or do I have to learn the whole gui drawing code and start from scratch? If I do have to start from scratch is there a book out there that doesn't assume that I know any of the advanced coding techniques or do I have to learn the whole language and then learn the SDK? I want to be able to write a program quick and dirty, here are the variables here's the formula, do your thing...
Android App Inventor.
The problem with this approach is that you'll learn how to make apps in App Inventor, but not learn how to program Java nor how to program Android. It may be useful as a first stepping stone though, or if you really don't want to get your hands too dirty.
Android App Inventor can do exactly what you want. I've done something similar with it.
One month and a half ago, it took some of my students (and some other random people) about two weeks to get the initial invite (once they had put themselves on the general waiting list). Since the form for the waiting list is still in effect, I assume it will take around the same waiting time in your case, or may be even less.
A couple of caveats thought: You need an Android phone to run your 'Android App Inventor' App (it won't run in the emulator). Android App Inventor doesn't give you access to the generated code (they have no plans to, besides they're not using java for the generated code anyway). App Inventor won't let you publish on the Market (although, they do have plans to add that capability, for now if you want your friends to install your app, it can give you a QRcode for it, or give you the apk file to share with them).
While you're waiting for your invite, I'd suggest you try out Android Scripting
http://code.google.com/p/android-scripting/
Scan the QRcode on that page (it's not on the Market yet). Install the SL4A app. Once SL4A is running, press on the menu key > select 'Add' > select Python. Once that's running, I recommend you run the test.py to see what kind of GUI intents can be launched directly from your phone python shell. The SL4A project is still very much alpha quality, but I'd say it's worth playing around with (especially in Python, since Python seems to be currently the most mature scripting environment supported on there, it actually supports a zillion other scripting languages).
Another thing you can do, from the Market this time, is to download the 'AndroidShell Lite' app. run the app, press the 'Local Console' button, and play around with that. I suspect that AndroidShell is using a version of the SL4A, but unlike SLA4, it can not be run in the emulator, the code doesn't seem to be open source, it doesn't support python (only beanshell which is just as easy actually), but it has much better documentation at least. That's why I'm recommending that you try out both 'AndroidShell Lite' and SL4A. Each has different strengths.
I believe App Inventor is what you are looking for. It is not designed for complex apps but can be helpfull for simple apps.
https://www.google.com/accounts/ServiceLogin?service=youngandroid&passive=true&nui=1&continue=http://appinventor.googlelabs.com/ode/Ya.html&followup=http://appinventor.googlelabs.com/ode/Ya.html
regards.
I will suggest you do the following:
1. Improve your JAVA programming skill. Reading material: "Think In Java"
2. Improve basic Android programming skill: any entry level android book
Make sure when you go through the material,
do your best to design and implement some useful projects, and publish to the market if possible.

Categories

Resources