 Hi, welcome to snakes on airplane mode or embedding Python in iOS applications. I usually say I'm Mukash I come from the internet, but since Ireland is essentially a Polish colony right now Like I'm from Paul's nine, which is in the Western part of Poland. I invite you to visit. It's nice Back in 2019 I went to London to give a keynote about how Python should not only be used for data science. It should be used in More ways like this Pew Pew console that Like I was using back then I got it from Radomir and in fact you could use it on this very conference at a workshop Earlier in a week, but I also talked about like WebAssembly, which is happening now and like mobile devices Which we're gonna make work now as well. So What do I mean by you know actually embedding and why do we even want this like this talk It's going to go through all of this. We're gonna begin with just, you know Kind of appreciating Swift the language that iOS developers do use for development As a kind of first-class language, then we're gonna say like, okay, so if you have Swift and like Why would you even want Python there, too? And if you wanted like, how would you actually make it to build so that Apple will be fine with it? running on an embedded device on an iOS device and if you already Have it compiled such that it doesn't work We're gonna talk about like how to actually use it from within the iOS app And finally we're gonna actually run some code for you to actually believe that this is all actually practically useful Before we begin like a question. I already got asked at a conference since this is the last day And we've been here for a while like it's like, why would you actually want this? You know the app store like has Swift and it had Objective-C before so sure like Objective-C has been with us since 84 And it's been used for iOS applications like you know, pretty much like from day one when The SDK was released It's been the language of the year in 2011 and 2012 and this is because of iOS like back then there were like three-quarters of a million apps in the app store which ended up paying out Over seven billion dollars to developers. So, you know, it was like two-thirds of the market for Mobile applications were iOS. So everybody was like crazy about Objective-C Only later they found out, you know, when they were actually trying to make those apps that they crash a lot And it's kind of awkward to write them because the syntax is very unfamiliar and whatnot and one dot So Apple saw that they're hitting a wall with adoption through Objective-C Being kind of like a legacy language. They tried to kind of make it more modern with Objective-C 2.0 which garbage collection that was later kind of Deprecated because it worked kind of lousily on a mobile devices But it turned out not to be the way. So they released Swift in 2014 and like four years later it already overtook Objective-C With adoption. So like apparently they did something right like it was the most loved programming language dubbed by the stack overflow developer survey in 2015 and second place in 2016 Clearly only possible because Python was struggling with the 2.2 to 3 Transition then but you know still we have to give it to them that, you know, most loved programming language And you know Redmon, Redmon.com says that in a world in which it's incredibly difficult to break into the top 25 of language-like rankings let alone the top 10 Swift managed the chore in less than four years. It remains a growth phenomenon. Okay, so clearly they did something right there But obviously when we're looking at this chart we're mostly interested in this. We're at a Python conference We're the first here like yes language of the year 2007, 2010, 2018, 2020, 2021 and good chance of doing it again for 2022 in fact like the top four languages there like have like nearly 50% adoption. So, you know kind of We're there. We're winning. So maybe we should just upseat, you know Swift and just kind of start doing mobile applications That way. Well, I Don't believe that. I think that Swift as the language of choice by Apple and make made kind of the Official platform tooling is always going to be better than using something that is third-party and tries to play catch up and like You know make stuff magically work in multiple platforms. Yes, there are multiple options for this. There's react native. There's flutter there's Beware that the Python community is working on But I personally just my personal opinion think that, you know, you should be using the best tool at your disposal so Why do we even have this talk then? Well, because I'm a heavy note taker Like I have like thousands of notes on my phone Like they're synchronized to my Mac and they do a bunch of things with them including like publishing my website And you know, there's a bunch of automation that I do all the time And I'm able to do it on the Mac because I just run the Python But I kind of do any of this on the phone and it's annoying because I already have those Python scripts Usually they're not even like none by you know, Nash and natural language processing stuff It's just regular boring Python like that uses the standard library and I still cannot use this So I found that there was an app on the app store that allowed me to use Python to you know Model workflows like IFTTT, you know and kind of write small scripts But it turns out like this was Python 2 and this is no longer maintained So it was like a kind of a cabama. I saw like what is this developer doing now? So, oh, they're doing Pythonista This is like an entire IDE that is running on your phone where you can do like mini apps and you know Kind of run them within Pythonista, but they can be graphical. They can interact with the Operating system you can have like widgets and whatever like is like, okay, that's cool also not made hained for like three years now, so I was looking at an alternative and Emma Cole Worked on and still works on pyto, which is sort of like Pythonista But actually maintained like and pretty powerful like on the website You can see you know that it allows for running Python 3.10, which is awesome Like, you know an actual modern version of Python You can run this includes numpy matlow live and whatever this is paid Which I kind of agree with her like you know it this entire pyto app is open source So you can kind of download it and you know try it for on your own But if you want to have like include libraries and a bunch of other things like you know This is a paid on on the app store Like the reason why is that if it were in the case You would have like tens of copycat apps that do exactly the same and then you know It's a support nightmare for the actual author. So open source. It's kind of you know We want it but at the same time like People don't play the game fair all the time So the fact that part of it is is paid is good by me But you know plenty of awesome features, but again, this is not what I wanted I just wanted a text editor to be able to automate it Like I didn't want to write my text editor within pyto and just run it as a pyto mini app Like that's that's not what I what I wanted Back when my haircut was quite different like I actually was saying that Python is runnable so the code like you know 13 year olds can program it. So this is what I wanted to do to let people write swift apps that are Modern that are first class on iOS but allow people Allow users to extend them to customize them, right? I can is this something that anybody is doing well as far as I can tell not on iOS But in the in the world of desktop applications that is a tremendously Popular thing to do you have like blender, right? Which is you know kind of industry grade 3d modeling software and it has a python API And you have bespoke synth like a module synthesizer that can be scripted with python You can just make it play notes. They're generatively and whatever if you're a youtuber or aspiring youtuber, you know OBS And that thing can also be scripted by python So, you know all of this is pretty popular in fact even production grade digital audio workstations like FL studio here can be Programmed with python so clearly the market is there people want this since like supporting Multi-platform python embedding. It's not an easy job So the fact that all those pieces of software industry grade pieces of software not toys like actual professionals use them the fact that they want Python API is there means that this is something that the users are Actually well wanting so should we actually unseat swift now we should make swift talk to python and you know The vice versa. So this is what we all gonna do next In practice in life because we live dangerously We'll be using be where's python 311 for iOS Scripting so like you know kind of setup Since python 311 does not build cleanly on iOS. I wish we're not there yet We will be there at some point. I do hope to work with be where to make 312 pretty much Compile for iOS with no changes. So we'll see as a developer in residence. I think you know I'm well positioned to actually make this happen We will be using python kit to talk from swift to python and vice versa That was originally created for swift for tensor flow That was a an attempt to replace python in terms of flow with swift and this is now a project that was Canceled so clearly again use the best tool for the job turns out that python is the best tool for orchestrating machine learning It's with try to unseat that didn't work But we will be using swift UI, which is the kind of latest and greatest in Creating mobile applications for iOS. The fun fact about this is that there's not going to be any objective C inside So before we actually look at code be aware be aware tries to make Python applications run on mobile in ways where you write a single code base and you can run it on Android and on iOS With pretty much no changes And there's also like a Linux port so you can run it on desktop as well We're not going to be using be aware today. We're going to be using like a Support element of be aware which allows to python the programming language be built In a way that can be embedded inside an iPhone application So what is this python apple support thing like why do we need it? Well, it is a gigantic patch file that makes tiny changes to many tiny changes to python Such that we can actually compile it with clang on on Xcode and make it embed But it's also like a make file. That is like a thousand lines long that Kind of provides like a ton of steps that you know have to be made for the actual artifact to be acceptable Xcode So with having all this in mind demo time cool so Oh Before we begin, let me show you something cool All the things that we're going to be doing we're going to be doing in memory because I have 64 gigs So why use disk? So let's switch to RAM disk see Python. No, we're not going to be working on CPython now I mostly have it to to run CPython because you can compile it in like eight seconds in RAM But now we're just going to clone an app that I wrote in Swift UI we're going to talk about it in a second. So let's go there See what we have here. Okay, so let's just open this bullseye Xcode project Okay, we have an Xcode application. It is a very very simple Swift UI application It's got an app object Which you can see that there's literally just some state You know we call it a game and there's a single content view I guess single window and a single sheet for like a leaderboard. So like very simple There's a bunch of views but it's only because you know, it's like an HTML You have a bunch of components that are like just nested inside one another So we're just switching them like, you know Splitting them into multiple files just so that this file the main file doesn't look terrible In Xcode you can actually see the preview of it. So let's wait until this builds and you know in a moment We should see yeah, this is like the the smallest thing you can call a game So, you know, it's like put the bullseye as close to the number 19 So you literally put the slider as close to 19 click hit me and like if you got 19 You get like many points if you got 90 you're gonna get less points because it wanted 19 Yeah, so like very simple application, but already kind of this is Swift UI. So it's it's proper iOS So like it works, you know in both orientations It will work in dark mode as well, which we can actually check here like by switching to the dark, you know And back and whatever so yeah, like all of this kind of works very nicely This isn't even the simulator Well, you can obviously run the entire application in a simulator I have my phone connected is called lucky 13 because it's an iPhone 13 mini and I'm lucky because Minis are just well-shaped for my hands, but now let's just use the oldest device that can actually run this So a simulator of I I pod touch which I don't believe they sell anymore, but you know, it is what it is So this is gonna take a while and then the simulator is going to hopefully show us something It doesn't show us yet. Okay, so we can you know kind of attempt to all 14 maybe here. I don't know 15 okay, so we got 149 points awesome and now we're in run two we can do five so fire is gonna be closer Okay, five. Oh 200 points because I perfect, you know bullseye amazing That was not scripted. That's just my talent Yeah, so it has like a leaderboard obviously, it's stupid You know, you don't log in you don't do anything But like it just shows you like there's a card that you can swipe out, you know It's like this is like a modern switch. Why doesn't have like many lines of code You can reset your progress if you don't like to have many points you can just start over that that's the thing That's the app like it's not very complicated What we will want to do is to replace this logic that like calculates the point with Python, right? So this is where we're gonna be doing So let's stop the simulator In fact like before we even do anything else like let's try to run it on the phone So that I'm sure that actually it does run because it will be awkward again a minute when if it wouldn't But it should be fine. So build succeeded and it says running, but can we see it? Well, I hope we can Look this is my phone from quick time player so you can actually see me manipulating the phone All right, like and it actually does a thing to prove you that this is actually the phone like this is the phone You know don't message me now Yeah, actually I don't trust him not to do it. So let's put focus do not disturb Okay, yeah, so this is my actual phone like and we're running this app right so like pretty nice developer experience We're gonna put Python there the reason why I'm so like, you know insisting on like oh look This is the UI. This is the simulator. This is the device is that all three steps fail running Python in different ways So I can show you that we can actually run it in all three scenarios Which is important if you actually want to develop applications later. Okay, so let's stop the application right now and put our Python Thing in it. Oh no, this is too small. So let's just change to this directory where I was before which was this Python Apple support. Let me show you what's in it. I See it on the wrong screen, but here it is. So it's essentially This make file and everything and it has a bunch of distribution files that we will be literally dragging and dropping into Into our Xcode project. So let's do it right now Oh, or maybe not like let's create some folders so that we organize things as civilized people do so frameworks So when we're gonna put the frameworks that we built And also resources, we're gonna put the standard library of Python. So boom resources I Can barely see it, but like, okay, hopefully, you know typos so frameworks and We want to put those XC frameworks there There's a bunch of them. It's kind of weird, but you know, like I'll explain it in a second So we were putting it over frameworks. We do want to copy them inside If you do that then github wants you to pay them because now you're You know kind of dot a files are gonna be very big and this is no longer free tier of github So welcome to for professional development This takes a little while to compile because it's like, oh, I don't know what this is Like it'll resolve it in a moment. In the meantime, we can add the standard library of Python Which is in this Python folder. You see like resources Lib Python 311. We also want that we will be using that So let's let's add it to resources Okay now after all those question marks disappeared we will Experience Xcode lying to us many times This is gonna be a story of failure But I want you to actually see this failures because if you actually Skip a step and end up seeing this. I don't want you to just see the final solution first I want you to see the failure so like we can actually know like oh like this is how this is all so now We put Python there that we don't use it yet at all. But like does it build? We pressed yes, and it says it does build it will say that it cannot run it because I have my phone locked But I unlock it and now it's running. Let me switch to the simulator later In fact, I want to do it because I want to show you that Python is not at all there yet So let's switch to like on touch it again this play Build succeeded awesome. It shows cool. How can we see if it's there? There's multiple civilized ways to do it, but we don't have time for that So let's see it in the simplest way ever Xcode allows you to see the build folder in finder. So we have the build folder again in the wrong screen Let's bring it here This we don't need anymore and you see that oh, there's a bullseye app It has like a you know an icon that Suggests that it won't run on this computer because it's an iOS app But we can look inside and we see that the bullseye Thing is 700 kilobytes big. Okay, cool. But the Python Parts which are like, yeah, the headers we don't need lip Python dot a is 70 megs big So if your application is 700 kilobytes, you can pretty much know that nothing happened like it This wasn't actually not included like it's it's it's not good By the way, you can see that those headers are needed for Xcode to show us things But what's actually linked is this dot 8 file. So clearly we have to fix something. So Well, we should make sure that there's a framework that is actually like included in this app. So is it included Xcode claims it is It listed under frameworks because it we put it, you know in a nicely called directory and it looks like a framework So it lists it. It says do not embed. We actually don't want to embed because what that would mean is Dynamically loaded libraries would be embedded inside our application. We have dot a files They should be linked with the binary that should end up like a single code object. So we don't want to embed That's fine. We want to link Do we link it in built phases? We should see link binary with libraries and they're all listed So they should link But they're not being linked. Why not? Oh, it turns out that I need to help with that from Corey Benfield If you look in that project, you will see like a lot of headers, you know So like all those dot H files and among them the main one that we want is python Python dot H If you open it, it will say entry point of the Python C API. This is what people include when they want Python So we actually do need to say this to the Swift compiler that like hey This is the dot H file that we you need to Look for to embed. So let's make a new file Create a module map Where's an empty file here? Empty. I just want an empty file. Give me an empty file, please. Oh empty documentation markdown close enough So let's let's put it wherever say bullseye and and call it module dot module map Okay, yes, not markdown dude. I'll still mark down. Okay We want to have a module called Python and that module will have an umbrella That's a nice keyword. I like that. They have a keyword called umbrella header Python dot age, right? We all we just look at it. We want to export everything from it. We're greedy and we want to link Python That's it. But we want to put it in a good place So let's show this in finder. There's more finders. I want to see all of them. Okay frameworks So those headers need this module map. So let's give it the module map What is happening? Okay, let's copy this file over here By the way, all of those things are the things that I hope with 311 are gonna be just automatic So but for now if you want to actually use this for the next two years, like this is what you have to do So this is what you will run on the device. Hopefully it's alive. I like, you know, you can hope And then we also have to make it work on the simulator By the way, what's fun? You will see that the simulators A live dot a is twice as big. It's because it's a fat library with runs on both arm and Intel So that's good. The device one doesn't have to have like fat binaries because it's only arm Okay, so with this in mind like that should already fix our problem, right? So let's try to run it stop the previous one run this one Built succeeded it did appear and this is still 800 kilobytes That's not good And the reason why is that the Swift compiler tries really hard not to link things in if it's not entirely sure It needs them because otherwise mobile apps would be terribly big And nobody wants to download hundreds of megs over, you know, kind of their data roaming in a different country So we actually have to in our app say import Python. What's this Python? Well, that's exactly this this thing that we just Defined this module that we just defined And we actually have to use it. So we have to define some fake function called please link Python and Here we need to use some part of the state C API like pie Initialize say like it's it's how you would initialize like a Python interpreter with the C API What is what does this say? Please think oh, yeah, of course Cool. So in a minute like this should disappear. Okay. Now. Let's try again See some progress. Oh Progress errors. That's good. That means like we went somewhere further. What are those errors? There's hundreds of them. But among them you will see that there's like stuff. Oh, SQLite is missing Why didn't be where added? Well, because it ships with iOS you don't have to actually add it again, like it would be wasteful again Your binary would be bigger than it needs to be so when you we link stuff We just need to say we want SQLite and in iOS we ship a SQLite version So let's add it and let me just already Lib Z is also needed. So let's add that too. So, yeah TBD by the way not to be determined It's text-based description also didn't know what that is. I asked Apple people and they said text-based description Now we're smart. Okay, cool. Let's try it again both succeeded. It runs and now 12 megs still smaller. It's weird Because the Swift compiler is actually smart and like within the binary There's a bunch of like debug symbols and stuff that actually does not link to Python H So we're not in fact linking those but we're linking Python now. So this is success now We actually have Python inside our simulator So we can start doing stuff with it but What we have right now is this import Python thing that exposes The C API of Python which is super low-level you have like stuff like Incrementing and decrementing reference counts and whatever like this is not really what you want to do if you're writing in a high-level Language like Swift so instead what can you do? There's a bunch of things for Objective C But again, we don't want to use Objective C and there's one cool thing for Swift called Python kit which originally came from The Swift 4.0 flow project so we can add it by just saying we want a dependency You can you also use Cocoa pods or Carthage, but like let's just do it like graphically here and you say from github I already have this here, but you know, it's like this is the entire link here Just add Python kit So you just paste this entire link here and say add package and now conference Wi-Fi will allow me to add the package It did which is amazing and we can see that there's already some code there. It's some cute like Swift code that does the thing that we wanted which is Like converting all the types to Python objects and whatever we don't need any of to do any of this We can just be users. So now Inside say our models, right? So this game Class that has just like, you know, what is the number that you should be hitting? What is the current score you have which round of the game it is it is it now and so on and so on? We can start using Python. So before we had this nice function points that was written in Swift We actually are like, you know, good programmer. So we even wrote tests for it So we know that if we get like bullseye We get 200 points if we get like one off like we're gonna have 149 because you get 100 extra points for hitting bullseye 50 if you're within five points And if you're like very far you just get as many points as the distance from the perfect score So we can run the test here with this nice button and instead of line 23 It'll take a while but be able to succeed it and hopefully is green. Yeah, okay It's green for now. We're gonna fix this now Because green is is boring Okay, so Let's bring up the game again. So this is Swift points. We don't want Swift We want Python and the reason why I wanted To show you this is that this the syntax of using Python inside Swift with Python kit looks Super nice. You actually have to import Python kit first. So let's do it. Okay, we imported Python kit But now what we can do is we can say that we will have Random and a random is a Python library that we're just importing so we imported a Python library And now inside Swift we can literally say that we want to say return Random randint How we never give a thousand points in the switch version So let's say between a thousand and two thousand so we know that we're actually running Python It will not be happy with us because what is it? Well a randint returns which we can check with Option click it returns. No, it doesn't return anything. Oh, it returns essentially like a Python object Right, so we need to convert it to an end. In fact, like let's see that it's like result So you you'll see what type it is Yeah, it's a Python object, right So we need to convert it to an integer so that Result Or like let's convert it right here and Yeah, and that's almost fine But if we put result here It will still yell at us because you know, it's Python. What if if you don't actually return an end? Well, then there's gonna be nil so you're gonna have to say well for this particular purpose We're not gonna do try catch or whatever. We're just gonna say Yeah, like it'll be it if it's not in and the app will crash. Okay fine But this is fine for now for like a short short talk like you don't do it in actual applications on the app Okay, so now let's see if this actually does anything Builds succeeded. Let me just press hit me and gives us like thousands of points This is Python running inside the Swift UI app. I am already happy with this This is already cool. And now what we could do is we could keep riding Swift using Python libraries This is not exactly what you want, right? Like we want to actually write like longer Python pieces of code so Even though this is cool. Let's extend this and just actually create File inside our resources. Oh, we're gonna be calling say script pui Annoyingly just like recently Xcode removed support for Python syntax highlighting because of the new parser So we're gonna be left with just like regular black Colors here, but yeah, we can do the same thing here. So like we can say input random We're gonna have like dev points, but you know, there's a guess of int and you know We're gonna be returning an int and here like we can return what? This random random int Thousand to two thousands the same thing but in a Python module. So, okay Like will this work like in spoiler alert? It won't you know, if you just replace this with script script and say script points Script points guess This should ideally work, but what will actually happen is something that we know from Python which is an Import error. Oh, we cannot find the file. Why? Well, because we never configured where our files are So let's do that if you get import errors You should tell your Python kit Where where your modules live? Usually you don't have to do this when you're using Python kit inside Like Mac applications, which is what Python kid was created for but for here You do need to be a little hacky like I hope to improve this in Python kid in the future But for now you really have to use some environment variables We're gonna be doing this but just as you don't import things inside functions in Python We don't really want to import things inside a Function here because what this will do is it will actually instantiate a new interpreter Where every college is like super wasteful for your battery and everything so instead Let's just say that we are gonna have our script, which is a Python object. Everything is a Python object in Python And here we just want to instantiate this But no there's no let anymore and this indentation is making me crazy Somebody should write an auto-formatter for this Yeah, cool So what we want to do is to find a directory for Python resources So the way you do it is just to say path is a bundle Which is what you actually have like this bundle with your app and like all those other files the main bundle Which is actually what ships and we want to have a path to a resource which is path for resource And we want to have this Python Slash resources directory. It actually wants you to say well of which type so what is the file extension? There is no file extension Yeah, so if we have this because like it can also be an L I mean it might not be fine a found so if it has actually found Then we want to set it Inside the Python path variable. You remember Python path. It's like, you know days of yours You know, maybe there should be virtual and no no virtual environments for this. Okay, so path Yes, and replace whatever you found so like yeah always set it Okay, this should this should disappear all is good. Cool So will this find our application now should if it won't I will be surprised Okay Boom it found our file again. So now we can actually code here inside the script, you know We can actually put more code we can now like do more imports. We can do interesting things But again, I set up all this phone and thing To show you whether this works on the phone and like spoiler alert. This does not yet work on the phone. We're close But no not no cigar yet so if you actually stop this and Want to run it on lucky 13 You'll be unlucky for a while. It'll do it. You'll do exactly the same Yeah, there's warnings. That's fine It'll be running. So let me show you that it's oh, it's not running current thread. No python frame It's like something is wrong. No module named encodings. This is terrible Why oh because it look live looks on my phone for this path this path will never be found on my phone Because this is my Dropbox directory on my On my laptop. Oh Okay, so what we need to do is We need to And that's in the app, right? No, we were putting the script in the game Yeah, so we need to also set Python home Which is the environment that actually says what's your prefix? This ideally should be program programmatically set and not the way. We're doing it right now This is hacky, but it will work for now and we'll work with Python kit so that this is smoother For now though, we can actually press play here, you know and build succeeded Which is good enough. It actually started Let me show you that there is an app on the phone right now. Okay, and if I play around with it I can actually hit it bad and still get thousands of points and I can hit it. Oh, maybe well Almost yeah, but see like, you know, this is this is the thing and there's like a leaderboard and whatever like, you know So we made it work now. It works on the simulator and on the device you can ship this Apple will accept it in the app store. It's good I could go on now if I had more time because you know now This is where the interesting stuff happens where you can just start coding in Python Like it's it's awesome But the talk was about the setup which took me like weeks to actually go through because there's multiple choices for each of The step which library you want to do Like build Python 311 with because this is 311 So you're one of the first people to see 311 working on on a phone but also Like, you know the thing is now you can do many many wonderful things with With apps so the some ideas for this would be, you know, you can Make users script their apps, right? So they could like automate some workflows. They could customize their apps right they could do like App behavior changes, right? So you can actually plug it inside some Swift code as you could see like the Python functions that we had in our script Now can be just called from Swift regularly with a little bit of help so that you convert Python objects into Swift native Data types Arguments from Swift to Python are already automatically converted, which is awesome. But the other way around you actually have to be explicitly Casting right so, you know, you can do change changes to app behavior both by the user But also by yourself so you could make some stuff like, you know Some bundle of code that you have on your website or whatever and just update it without you uploading to App Store Is this is this elegant? Maybe it does Apple love this probably not but do people do it like I don't know Facebook like yes, they do Smart content templates so for example, if I create a new note, but I'm in gym I could have like a gym score note and it will automatically pre-populate with stuff that I should do today Or like level definitions in games like this is in fact what like civilization for use Python for so, you know You could actually just define Levels using Python code, which is nicer than just using something this static Generating music sequencing again like you know for music just saying I want this note to play after this note wait this much and then play Another note like plenty of ideas that you now enable users to do because you write an application in a first class language But you enable script ability on the user end So yeah, I could just go on but I'm out of time two minutes So let me just say that what we just accomplished was we ran Python 311 which is in beta That we build ourselves I didn't actually build it right now because it takes like 15 minutes for like all those Xcode stuff But like you know, we use the dot-aid files by make iOS from Python Apple support by beware Inside a Swift UI application on an actual phone as you could see with no objective C boilerplate needed So it was all swift And this would not be possible without the help of Russell Keith Maggie who made beware happen. So huge help from is and like I love the guy. Thanks Cory Benfield actually helped me convince the Swift compiler to statically link with Python a without him This would be a theoretical talk. This was a practical talk. Thanks to Cory who works at Apple now on Swift on the server side Ray Wenderlich taught me iOS like those are very good tutorials that you know are kind of directed towards people who are new to programming But also are not super annoying to people who already know some other programming. I'm so recommended Pedro Jose Pereira Vieto wrote Python kit. Thanks to him Ned Deely takes care of Mac OS and Python including installers that you know, people are using when they are downloading Mac OS binaries from Python.org Lawrence Dana helps bootstrap Apple Silicon support that we just used back in Python see Python 3.9 So he actually is an was an Apple employee at the time and you know contributed this to us So thanks to him and Ronald Osirin wrote Pi Objective C Which is kind of the precursor this granddaddy of you know connecting Macs and Python And help bootstrap Apple Silicon support by reviewing and making his own changes as well So thanks for all of those. My name is Lucas Langa. Thanks for coming. I hope you liked it Thanks very much Lucas for that great talk We have two minutes time for questions. So let's see. Do we have any questions from the audience? Please step on the microphone. Great talk. Just a quick question your dummy function Please link Python. Is that still needed at this point when you've imported it? It is still needed like ideally We will have like a more elegant way like of forcing linkage for example buy Python kit actually doing something But like you need to convince the Swift compiler that Python is needed. What Python kit is doing it is actually like just Trying to find symbols inside your binary, but dynamically by DLCM So within the code of your application, it doesn't look like you need Python.h So you need to really just force it somehow Can we have the next class question, please? Thank you for the talk. I was wondering about if you can pip install things iOS and especially the binary package No Not in this way of embedding. Yes in Python, but also no So there's this is really a long question. So like very short answer We could make pip work for Python pure Python Dependencies so that they could install into some local storage and you could import them with some Python path Mangling that will be possible for stuff that has any binary packages. You need to either compile the Dynamic libraries yourself because you need to sign them yourself. Otherwise iOS won't enable you to You know kind of load the dynamic library. You will have to do it yourself Or just actually included in the static binary as Python is doing Python is also including everything inside a single binary because it's just kind of It's it's more. I don't know like it's it's a sure way that like the thing will work So yes, like iOS now allows you for dynamic libraries, but you need to sign them yourself So wheels from pi PI they cannot work because they're signed by somebody else or as is the case now They're not signed by anybody Thank you, and we can do another short question It's very impressive to see Python running on iOS Do you know the performance is like compared to running on say Mac OS laptop silicon? Yeah performance is better than on Intel max it is weird, but like M1 in particular, but also like those like a processors are predecessors of M1 is very highly optimized for Automatic reference counting which is part of Swift and it just so happens that Python also uses reference counting everywhere So like for some reason like Python is pretty quick there Obviously it is like an order of magnitude slower than Swift because it is using an interpreter loop to interpret your code in real time That is kind of the point because we are providing this code on the user level, right? Like we could make like an edit box and the code could be literally provided by the user But it's still faster than Intel max like you know, it's it's it's pretty impressive So, thank you very much. That's all the time we have now. Please give another round of applause to Lukas