The Quest for the Pocket-Sized Python by Christopher Neugebauer

0 0

thank you very much lovely to be here my talk is called the quest for the pocket-sized Python it is fantastic to be here for the first time at PyCon Australia as a speaker I I've been I've been told I'm an organizer this year I'm not an organizer do not believe anybody who tells you this but that is pretty much the extent of the Python stuff that I've been doing for a few years now because my professional career is as an Android developer and this is kind of sad because I work I work mostly in Java as my programming language at work which is terrible but my job is basically I work on a large scale multi-platform app this means that i touch code on Android iOS Mac OS 10 windows and if if I if I get told to I write a bit of Java services code as well which is kind of sad I cry myself to sleep quite frequently so last year at PyCon canada i gave a talk on the state of python on android and a couple of months later various online for picked it up here's his reddits android section this comment here summarizes the state of that for the results of that talk in a bit of a nutshell my talk was upsetting I concluded that there isn't really a good solution for making native Android apps on Android or iOS games absolute custom user interfaces are a bit of an exception so you might be wondering has what has changed in one year has anything changed no sorry so today's talk hopefully things that are a bit less upsetting than the talker gave last year which I do strongly recommend you go and watch or read the slides for I'm going to focus mostly on people who develop real Python stuff web developers other Python enthusiasts I'm not going to cover too much about making real native app stuff like that I don't think the support is there if you want to go and develop native apps that run on Android run on iOS go check out kevy go look at brendans talk use your time machine so I'm going to talk about a couple of things mostly developing web applications and what we can do about making pure python apps that run on your choice of mobile platform and something that I was going to talk about that don't have the time for is this thing that I call hybrid approaches but to start things off we'll recap a bit about what we're talking about today so mobile devices they're a thing so I had one of these for a while it's a pretty representative mobile device basically it was a it was a device that make phone calls and send some texts it in fact had an app as well snake was fantastic but that was really the extent of what phones and these sorts of things were in the early 2000s we had this thing called the pocket pc was like a pc but scaled down so it would fit in your pocket and well this was a bad idea as well because the sort of thats what we made for these things well that's like a spreadsheet graphing chart thingy program but scaled down to actually like operate those buttons down the bottom you need a stylist to operate it and these aren't really devices that we care about anymore because you know they're all 40 use so mobile programming is we know it's basically came around in 2008 and there's three aspects of them that area that we really care about the first one is capacitive touchscreens so you can actually use your thumb to operate it you know devices you can use with your hands now these require new user interface frameworks beyond what we had previously because design approaches to that older you I talk it's talk talk up didn't really cater for mobile software based keyboards means that we have to deal with you know the available amount of content on our screen changing very frequently and it also makes for users that really hate typing because software keyboards are generally awful we also lose precision from from mouse pointers because we have to use our thumbs instead of curses so you have to make our designs less dense and make points of interaction really clear and from a Python developers point of view we have this problem of app stores which basically create rules for what things can be installed on a given class of device now on both Android and iOS app stores act as the only way of discovering which apps are available for a platform so you might be wondering where Python fits into this whole situation so as with my talk last year everything is still pretty bleak so iOS lots of people use iPhones use iPads for a very long time any language that was not anointed by Apple was banned from the App Store this was sort of as a way of getting rid of flash on on iOS but languages like Python like Ruby all these other languages that aren't Objective C or C or whatever were basically banned from from iOS as collateral so Python was collateral and these days who saw the WWDC keynote back in June so these days Apple a kind of hostile towards Python basically they developed a new language called Swift and then they created some bogus benchmarks that basically made Python look as absolutely dreadful as they possibly could make it like doing complex objects sorts whatever that means or doing cpu-bound encryption which also is not particularly good in C Python so iOS is kind of sort of hostile towards towards Python as it is and the other platform that we have what we have Android and that's a bit weird we know a lot about Java the Python community knows a lot about Java jython is a thing it works really well but Android doesn't actually use the JVM it uses this weird thing that's kind of sort of like the JVM but not quite called dalvik which basically means that Jason doesn't work on Android and you can't access Java API on Android it's taken people who aren't from Google because Google you care about putting Python onto onto Android it's taken them quite some time to figure out how to make things work and since people have figured out how to make things work well Google have gone and changed things again which is very kind of them making it really difficult for us Python developers to actually get stuff onto our phones now the people who come from the web the system's world we have the privilege of being able to choose what language we use to develop now we're all set some people we like Python but in the mobile world things are odd languages are anointed as official api's tend to be written in really specialized calling formats that require the specific language you have and re-implementing the api's in some other language would be time-consuming difficult and something you would not want to do unless you were paying someone quite a bit of money Mobile is also interesting in that the platform's have consistent design standards so official ap is which require official languages make it easy for you to get at the design standards that each language each platform users and people who are implementing alternative toolkits have to play catch-up which is kind of dreadful as well and finally there doesn't appear to be any real work in making Python happen on iOS or Android coming from any commercial entities it's all happening on people's spare time in the community this means people have specific needs that are filled rather than trying to solve a general problem that will that will solve a problem as a whole it's not necessarily a bad thing but it's it's at odds with the mobile industry people are basically using tool kits that are out there people aren't interested in in making tool kits by and large so in summary mobile is not a thing that that resembles any other thing that people have been developing for and python has been artificially disadvantaged so I hope you're all happy with the state of things the world is a wonderful place so the rest of this talk is basically a grab bag of things you can do without actually coding python or making native mobile apps in python and i'm going to take a guess most people here are generally interested in the web because they're the people making consumer-facing stuff am i right in that assumption web developers here cool a reasonable number of you so this is a quick overview and some pointers as to how web developers should approach mobile development now if you're an astute observer you'll know that I'm not actually talking about Python on mobile because if you think you're a Python developer doing web front-end thing is you're also delusional because you're actually writing JavaScript so what I'm talking about is basically what you're doing with desktop web you're writing a service code in Python and you're doing HTML and jes for the user interface so the key difference between designing for full sized computers and mobile devices is to actually consider how your design will work in a touch context now touch is interesting because you have basically meet pointers as opposed to as opposed to a mouse so this is a picture out of google's design guidelines for android basically touch screens they don't have a whole bunch of precision for touching things and it's difficult to tap things precisely so designs that pack a whole bunch of things into the same place are really difficult to use so this is a thing that google call the 48 pixel grid they don't actually call it that i call it that but their name is a bit weird in this design basically having touched targets to the divide that divide your grid up by 48 pixel blocks actually gives you a clear amount of spacing for your your widgets and stuff like that so you can see that button there is about 48 people or 40 pixels high it has some spacing to make sure that you don't miss it this is the thing you should do on your mobile websites it means that you don't create thing by if you're densely packing your hyperlinks on on a web page for example it's very easy to accidentally touch the wrong thing so there's a rule of fun keeping your tap targets 40 pixels wide 40 pixels high makes things easier to tap now cell phones are operated with hands I don't know about you I can't see through my hand it's a device I can't see through so basically if you operate a thing with your hands you generally can't see the things that are under your hands while you're using your phone so designing navigation structures so that things that you don't need to see the content for goes up the top means that it's easier to or it makes more sense to put those things there and things that you do need to see the content for down the bottom the final thing that I want to talk about in design who hears heard of responsive design who here thinks that's a good name for it okay good so I call this what I think it actually is which is constraint responsive design which is basically dealing with dealing with the available width and height of a device and changing based changing whenever the the available constraints change this is kind of important for mobile because keyboards appear and disappear quite frequently because people change their phone from portrait to landscape quite frequently and this drastically limits the space you have to display things so here's an example I have a android phone this is in portrait mode it's great for showing long pages with lots of height but not white images you can see a whole bunch of stuff is cut off and if i put my keyboard up well I lose about a third of my height and I swap over to landscape well I can see more of that picture but then if the keyboard pops up I lose sort of half of it so if you're doing design stuff you should make sure that your design works in basically every every orientation you have it with the keyboard up with a keyboard down responsive design is kind of important for that the final thing i want to touch on is is discoverability of web apps web apps have a kind of a big disadvantage compared with absolute a package for a mobile platform the web is hugely in accessible on mobile devices because software keyboards are difficult to type on coming back to pages is difficult because it's difficult to bookmark things and navigate bookmarks on mobile devices so you kind of want to be accessible on app stores and to make a point here I'm going to make an app shortcut from chrome for a for conference that i was at last week so to do that I have to open my browser I have to type a URI when the page loads and then I have to press a bunch of link target's to find a schedule and then I have to press the button here and then this really obvious button that says add to homescreen is the one they need to press and then if I forgot to add a favorite icon then the shortcut that I get on my home screen is this really obvious our page with the world on it as opposed to like a nice icon this is kind of awful the alternative is of course making yourself a nice app that goes in the in a in an app store where you search for the thing you want you select the app you want and there it is so if you want to be found on them on mobile and you absolutely insist on using the web you should make it easy to find your apps it's not that hard and it will get you get you more users so you can create a simple wrapper that puts yourself in the Play Store or in the App Store on iOS and that's sort of half the way half the way there for bonus points you can make your web app behave a bit more like a mobile app so the first thing you can do is to wrap a simple app write a simple app that basically takes a webview and loads your your page but it's a bit lazy people don't expect your expect mobile apps to work like that you need to meet users expectations apps have a certain set of implied behavior that most web apps get wrong this is things like resuming state when an app gets killed being easier to navigate forward and backward keeping 8si being able to remember you where you are and working offline so basically things that you need to do include remembering stuff off session across sessions like remaining logged in even if an app gets killed they should remember where they have were so they should launch again to the same page as as they were in when that gets killed because if you if you change apps on a mobile platform sometimes they get killed by the OS this is a thing and and basically you can do this by ensuring that every state of your web app has a unique URI and for bonus points they should function well off line using like HTML offline support tool kits like PhoneGap can be used to write full-featured wrapped html5 JavaScript apps but they can also do basic web view wrapping like I've just described it might possibly be a bit bloated here so the message here to take home is that you know lots of people may say nasty things about web apps in mobile but the truth is that if you play nicely behave like a mobile app then your users won't care that you're a web app so go and do it it makes your apps it makes your products easier to find so the next thing I want to talk about is the pursuit of basically apps written in Python running on mobile devices and when I did my survey of of Python on mobile last year I saw a bunch of contenders including including pie game including cutie but there's really only one contender now and that's Kevvy Kevvy is a kibbeh is the only thing is making progress in terms of making python run on android run on iOS now Brendan Scott just gave a really extended talk on kevy also there's quite a few talks on that that will cover Kitty better than I ever will so if you want to do that go look at those talks so for those of you who want the quick overview of what Kitty is it's really two things there's firstly a UI toolkit with custom widgets that work across multiple platforms with a bit of a focus on touch applications so people really think about this in terms of deploying python activity using heavy to deploy python on iOS to deploy it on android and then there's also a series of projects for embedding and deploying python on mobile platforms so kitty is pretty fantastic it's useful for doing a limited subset of mobile tasks kitty is really good at doing completely custom ui's so if you don't care about making your app match the native look and feel of your system then Kitty might be good it and get your cross platform app quite quickly this is a good thing if you're aiming to get so deterring my notes better so Kitty is not something for making native you eyes on mobile platforms it implements its own widget set and it does not accurately replicate the feel of UI elements as they are on their own platform so it doesn't implement the look and feel of iOS it does not implement look and feel of Android and if they do they're basically doing manual replication they lag behind other platforms they won't feel quite as good as the official widgets the exception here is where you're doing custom ui's so if you do games for example out where you an angry bird does not need to look like a button so give is a perfectly good platform to do this sort of thing with and you get to do things in Python which is really nice QB says it will run on it'll make the same app run on Android iOS Windows Mac OS 10 but you need to consider whether or not an app that works in a touch context will also work in a desktop keyboard mouse context at the same time just because something will run on multiple platforms you should consider whether or not it should one thing that is substantially improved over the last couple of years is access to hardware api's on mobile platforms people care about doing things like geolocation you want to know where you are when your when your mobile accelerometer access things like that these are things that make mobile device is more interesting than desktop devices now the kitty project has been the primary driver of this they've produced a couple of projects one called pi genius and one called pi objects these are tools that wrap up complex interfaces on each platforms also they provide access to complex interfaces on each platforms preferred language so PI genius gives you access to jut the Java native interface on Android it allows you to wrap java classes in in in Python really quite quickly and pi objects does the same thing with Objective C so to give you a sort of an idea of how this works this is my first and probably got one of two code slides in this talk this is code to to wrap a to wrap an onclicklistener in android so you basically say that this is a Python Java class you say what Java interface it implements and then you go and implement the jni methods that you need to the objective-c version is also quite similar this is PI objects wrapping NS string which is the string class and Objective C it's quite easy to do but you'll notice we have this funny trailing underscore thing Objective C has a really weird method calling syntax where you interleave parameters with parameter names it doesn't quite translate well as a Python so there is a way to basically get to get to native Java and objective-c classes within Python but it requires a lot of boilerplate another kitty project that's coming around in just the last year is a thing called player which is basically a simple way from streck ting away various pieces of hardware so they provide an accelerometer class in Python which in turn calls the Android accelerometer class on on Android using the using the jni and does the same thing on iOS using a consistent interface so you can make cross platform cross-platform mobile apps that don't need to spend a lot of time implementing both the device ap is on each platform the final frontier of mobile dev in Python i think is to access each platforms native UI libraries it will be great because it means i wouldn't need to code java in my day job so the obvious candidates here are pi genius and pi objet but these are not quite there yet they seem to be aimed mostly towards supporting TV apps the bits of Android and iOS where gooeys come in are not terribly supported well bye-bye these projects yet so for example floors on Android if you want to access read-only classes these are ones that you don't need to extend you'll find there that will get you most of the device API access but if you need to extend classes you're out of luck so Android users manage classes that you need to extend in Java you can't do that from Python just yet and on iOS the interface builder actions and outlets don't work in PI objects so you can't you can't connect I OS native interface builder you is one interesting thing that I saw in the iOS 8 beta is that there's now actually a Python framework that you can deploy to iOS so while they were slagging off on python in the WWDC keynote they actually released python for iOS which i thought was kind of curious i have no idea what this means but i think i think there's something to be read into that i'm not quite sure what it is we might know in a year or so yeah I have no idea either again one thing I touched on in a longer version of this talk is something called a hybrid approach which is where you write a substantial part of your core logic in Python but write your user interface code in your platforms preferred language if you separate us if you produce a really rich web api for example that's one way of doing it so you make HTTP calls to a python service and it looks like thanks to kivi it might be possible to embed an entirely python services layer onto a native app and just write your UI code on your preferred platform have a chat to me later a bit more if you want to know a bit more about this I do something similar in my day job just not with Python so this brings us to the end of the talk there hasn't been much real progress in getting full apps that fit in with the rest of the system working on Python and this is sad but there's still a place to the Python coder in the mobile world so firstly we know that mobile sucks in general and Python support on mobile also sucks but it's still possible to write lots of Python on your back end and make your front end in other languages that's the end of the talk as far as scheduled time is concerned I have like five minutes for questions I guess yep you have five minutes fantastic questions I'm entirely surprised by this first questioner yes um I would never ask a question and the user we talk to particular about Android but also in iOS about whether it's the limitations here are they technical impossibilities or just a matter of man-hours and women hours of resources being thrown at the problem okay so in the case of Android and the jni there is a fundamental technical limitation in terms of extending java classes over the java native interface that's something that will not be solved without doing some level I suspect of code generation during a build phase so producing something that will implement the subset of methods that you want to override on the Python side but giving an interface so you can implement it on the on the Python side which is basically how pi genius works in the case of iOS I'm not sure whether or not that's a technical limitation the PI objective-c bridge on Mac OS 10 definitely supports the interface builder stuff I'm not sure if there is actually a limit on on iOS though okay um to that end in about 20 minutes half an hour's time I'm getting a lightning talk which may be relevant to your interests if on that particular topic so on the iOS seidel were maybe I'll just leave it at that shell oh okay cool alternatively you can get someone working on python that runs on dalvik they're saying well yes Richard yes hello I wonder if you could just touch a little bit more on this concept of having a native UI with a Python service okay so C Python will definitely run on Android this is how QV works it will run on iOS once again that's how QV does its thing I suspect it should be possible to call into Python from a from a Java activity because once again kivi somehow needs to bootstrap and Android application keep you somehow needs to bootstrap an objective-c application in order to launch the Python interpreter there could be an avenue for extra work of so for work here in actually making a rapper that will allow you to call into Python from the preferred language of your platform I haven't seen it done yet but everything seems to be there it just is something that hasn't been done but it should be Tennessee I'm not ask a really ignorant question I think on Android the ideas that the applications in theory is supposed to be able to basically provide services to each other right couldn't you basically do a two application process where one applications in Python providing services and another one's and like a Java app that consume services yes you could theoretically do that the question is how you sort of provide the interface and that's an unanswered question at the moment like HTTP might be a way to do it badly but that could work as a way of doing inter language communication yeah so I've used could / or phonegap yeah sure with web applications and I'm just wondering do you think there's giving it those work going on with transpiring Python across to JavaScript that that might be another way to develop cross-platform apps with Python yeah if you're happy to tolerate if you're happy to tolerate a web browser as your as your front end then absolutely that would be a good way to do it go see Ryan Kelly's talk tomorrow on pipe ijs for more on there this is yes so if you're happy to tolerate the web as a front end then that would be an interesting thing to do it just depends on the power of your of your phone to interpret the massive block of JavaScript that is your Python interpreter it should work right now it's just whether you would want it to work or not is a different question any others any other questions all right oh you have one minute ish thank you everyone Chris you