Back to the video

Will PyScript replace Django? - What PyScript is and is not

Transcript

By Cheuk Ting Ho. From DjangoCon Europe 2022. Published October 14, 2022.

Watch the video Markdown version 3,198 words · auto-generated · en

Summary

PyScript is a framework for running Python applications in the browser, using Pyodide, WebAssembly and Micropip. It works alongside JavaScript rather than replacing it, and can make scientific and data-processing libraries such as NumPy, pandas and scikit-learn available on the front end. Running computation in the browser can reduce back-end load, provide safer code sandboxes, and keep sensitive user data on the user’s machine. The speaker argues that PyScript will not replace Django, but can complement it. Their example combines a Django server with a PyScript front end to run a movie recommendation model in the browser: Django serves the page, static data, model files and Python dependencies, while PyScript loads the model, processes user input and updates the DOM. They also explain that PyScript was still alpha software, with limitations around Python and package-version pinning, browser support, HTML generation, and packages containing non-Python code.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction

Well thank you so much. So thank you for having me here. First of all, I I'm not a Django developer. I have finished the Django Girls tutorial I have finished I have finished the tutorial on the documentation side and that's it. That's my Django experience. So today I'm not going to obviously teach you Django but I'm going to tell you about PyScript and some fun facts about it and also a little demo to show you how you can use PyScript. So very exciting stuff and that is the how you can get the slide. So I always put it there at the beginning and I always spend some time talking about other stuff so people can take pictures of it if they want to find the slice later.

Also attack me on Twitter. I love people tagging me so yay so how many of you here have heard of PyScript? Yes, ooh, we're getting popular. That's great. That's great. A lot of people heard about it right now, I think you know, back a few months ago there won't be that many hands and people will be like, what is PyScript? And now people know about PyScript. So But before I jump into PyScript, I would introduce myself. I'm Czech. Right now I work for Alaconda as a developer at Africard. So if you have any questions about data science stuff or PyScript stuff, you can talk to me. I will try to help you. If I can, I will find the right person who can help you. So I also involved in a lot of open source projects.

1:34 PyScript Fundamentals

I love open source. That's why I am here in the community. And also contribution is not just code. I also organize a lot of events. So I hope That's also Cantus contribution as well. And I stream a little bit on Twitch when during the pandemic because I was too bored. So what is PyScript? So a lot of you already heard about it. I'll just quickly go through the checklist so make sure everybody is on the same page. So what is PyScript? PyScript is a framework So yesterday I gave a live stream talk about a conference called PyJamas and people asked me, is that a framework? So I think nowadays we need to be clear what is what, right? So PyScript is a framework PyScript lets you run Python applications in the browser.

So it's the front-end stuff, right? You know, it's just that, you know. And even though it's running on the front end, it's not try to replace JavaScript, you can use it with JavaScript. And because it's using PyODI. So Py ODI actually kind of have a lot of APIs that you can convert Python objects to JavaScript objects and vice versa. So Because PyScript used PyODI, so there's a lot of operations. It's kind of very similar to using JavaScript. For example you can create some event listener or you can have some things that you can con you know have interaction with the dome and other stuff and

Pyce Group is capable of doing that because now we can have you know the Westum which is the WebAssembly so we can interpret Python into WebAssembly. So PyScript's actually standing on top of the shoulder of the giant, right? So we have a lot of amazing projects in the Wasm and PowerDye kind of things. Also the micropip that is using we use micropip for a lot of things as well. So those things empower PyScript and it makes a lot of popular package available. So a lot of computational stuff, the scientific computational stuff like scikit-learn, pandas, numpy, those

Available. Also other like visualization stuff that data scientists love. So this is a very good tool so I think it's easier for me to explain it if I show you an example. So this is how to use PyScript. This is how you use PyScript. Yeah, so it's just a script. It's just JavaScript, right? Just like put it in there and now you can write Python. This is that simple. So this is a hard world example. So it just generated this page. I just put it in as an iframe. So it's nothing special, but this is generated from Python. This is why it's a hardware example. There are actually more examples

in the official website, also all around in the internet. So you if you are interested you can find more. I also have an example. Later I will show you. So you see that this right, like this you have to oh you have to call it in from the PyScript. net. Oh also by the way it's in alpha, so you know, take it with a grains of salt and you know it's not very stable yet so prepare be prepared that things may change and actually you can help the change by just giving your feedback and stuff. So this is you call in, right? But what if I want it to be more stable? I want to, you know, because it's enough, things may change. I want it to be stable you can self-host it

5:26 Why Run Python in the Browser

Just like other things that you do with JavaScript. So I can actually show you in my example how you do that. So I've talked a lot about PyScript. Why I'm talking about it, why like why we care? Because I have other stuff that I could use. I have the HTMX, someone told me, that you can do things in the back end. Why do I have to excuse me? So I don't have to do it on the front end. Excuse me, choke

on the water. Because sometimes we need to do things in the front end like we do all the time. So resemble some stuff are computational heavy. If you have lots of users, you can't always do things in the back end because things scale up Not as well if you would do everything in the back end. Also

Sometimes you know I've a lot of people tell me that oh actually PyScrip is nice because Now I can let people try out stuff, right? Big because before if you want to create a sandbox for people to do it try some code and stuff. It could become a crypto mining playground for people to run code that may not be something that is what you intended, right? And also sometimes you just can't do it at the back end because some user they may have some data that are too sensitive, that they are not going to submit it through the internet to your back end You have to do that in their own machine because you know

7:44 PyScript and Django

it could be some personal data that they should not share with some other people. So it has to be stay in their own machine So, before I show you the demo, we'll PyScript replace Django. The answer of course is no. So that's actually a clickbait, so that's why you come to the talk. But yeah, if we Put this stuff together, we can actually make some amazing things if you we use the PyScript together with Django. So Here's my examples. I'm gonna spend the rest of the time show you what I have done. I hope you would find some use for this So all the code hosted on GitHub, you can have a look at it.

8:30 Movie Recommendation Demo

I'm gonna show you what I've done. So now I have a Django server running. It's a bit messy here, but I hope you see that. I'm now running a Django server. So yep, we get all the static file. I'm gonna explain to you what happened here. So this is the index. html. So what happened? I would first just show you how we build the model. So what this example is doing is actually recommendation thing, so maybe I'll show you this first. So favorite movie, I put Iron Man Yes, that's oh. Typo, typo, okay. So I would have let's say five recommendation

I hope you can see it. It's a bit small, but I don't know it whether so it's running actually. Yeah. So I have five movie that was recommended. So I can continue with this, you know. Oh give me more if I like this one. Which is I think it's Batman movie, right? The Dark Knight yeah, so I have woe ye now. So let's just go on and on and I can go from woe, what are we gonna get? O ye from Maybe some robot things. Don't know. So this is the recommendation engine. So this is actually just run on the front end. So we'll show you what I've done. So you first of all you need to get the data.

10:01 Recommendation Model Preparation

So I got the data from this Lens movie lens dataset. So It's all on the RIGM if you want to see the GitHub repo. So you download the CSVs which contain all the ratings from all the movies that they collected So in there, I have do some things. You can do better than me than this. Like I'm just using some command line to load them into the model, but if you have something more automatic you can do that. So load the CSV into the model and then you train the data. So I've done this with again a command very basic. You can do something more sophisticated than I do because You are better than me at Django, so you can do this.

But anyway what I've done is to use the CSV to create to model that I pick code. So there's actually two things here. One is actually just the title and the ID mapping The other is actually the actual matrix that are used to get the recommendation, to get which movie are closer to which one. So if someone put in a movie name Then I would get recommendation for the closest X movie. So for example, five I just show you, or ten or eleven. So this is how you do it If you want to know how to create this recommendation engine, actually I'm running a workshop on Saturday, so you can come and

11:35 Deploying Models and Packages

come and try it yourself. But anyway, so this is just preparing the data for use. So after this, the tricky bit is to deploy it to a to for the user to use, right? So after that I'll actually dump them. But where I dump it, it will be here in the step. No, don't load it. In the static file, because they're too big. So in a static file you have these two pickle. Again, you can use other format. Pickle is not the best, but I'm just using it for now. So you put in the static file and it can be consumed by PyScript. So in here this is the template for the index HTML. So I'm just calling it all this file that I need.

So let me explain pynth here a little bit. So in pynth, You have you have a few files. Excuse me excuse me one second. So I host all these files. So I have the Python file. This is where the model sits. And there are also the model that I've trained. So they are all like hosted here. on the static files. One extra thing that I because I need some Python packages to do this, right? I need the Psyche Learn. Which is how I deploy the model. And I also need this fussy woosy library.

So you see here that's a two things that I've done differently. So here you see the in the static here that actually I can host the wheel by myself to the environment here you can do that with most packages, except those that are not purely written in Python, which Cyclone is, because Cyclone also involves a lot of algorithms that speed up with C code and other stuff things. So I have to call in from Python. Otherwise you can all host it on the same server if you want to. So you have a close environment. But unfortunately psychular and you can't do it yet.

13:52 PyScript Implementation Details

So you have to call it in So these are the environments that we need to run the your Python code. So your files, your models, and the packages that you use. So you all host it there. And then you have your script. So this is just Python script. This is exactly like how I would write a script to deploy this model on with Python So here I we can see that I have also do something for a hacky thing. For example, here in HTML, I mean PyScript right now, you can't write HTML into your You're for example I want to write into the output here. Right now you can't do it directly because the HTML attack will be removed for

safety reason. So I'll just do some hacky thing here. I replace some other thing with the bracket so I can create some HTML code. I can put it here. But yeah, so if you have any suggestions that PyScript should do it, you know, please there's actually a discussion thread on GitHub so you can Chip in if you want to. So this is the this is basically you just run the code. You take the user input, right? And you take it and then you run the model and then you so this KNN recommender is the where it happened so it stayed in another Python script that I put it again in the static file.

So after that like you push in the model you have the your KNN recommender And then it would just find the top end movies, it would give it back to you, you can just write it back to the DOM. This is how I do it here. So this is basically what PyScript looks like. If you want to, again, if you want to know more about it, talk to me. You can also find this code on GitHub. So here it's all on here. Have a look if you want to. So the link is here. Okay. So There are more examples. These are more visualization focused example here. So

16:11 Questions and Future Directions

have a look if you want to. QA time. So far I've been talking about PyScript and a lot of people ask a lot of questions. And I want to add more to this list. So if you have any questions, please ask me, then I will put some of them here. So I will keep us answering some questions. First of all, can you pull in a Python script? Yes, I've just done it in the example so you can have a look. What is the Python version that we're using? So PyScrip is actually using PyODI, like I said. So whatever PyODI is using right now, the latest, it is the Python version. So you have to check with PyODI. Is there any way that you can pin

a Python version? No, not a simple answer to that question yet, so we are still working on that. Why can't you use the tag, you know, just like JavaScript, right? Script and then Python. You can't because most browsers are not supporting that yet. So that's why PyScript has to use a custom defined script that is Py Python script. So that's what we're doing right now. Again, it's in alpha, so things may change in the future. Why don't you just use PyOD? It's like this analogy is more data science-y, but I would say that it makes using Python in the front end much easier because in Pyodai

if those of those of you have tried Pyodai there's a lot of APIs that you have to have some understanding of how you know the front end work, how you can work with JavaScript and other stuff. With PyScript, if you just have a simple Python script, you can just run it. It's not a big problem at all. So you can do that. And someone asked me a question, like can you pin a packaged version? Right now there's again no easy way to do it, but you can host the wheel yourself, like I did if you want to. But that only applies to those that the package that's purely written in Python. If it's not, then you may come into a dead end and you have to see what the maintainers offer you in the future

or you can chip in to help how you know we should do that. So that's basically what I'm going to tell you right now today. So if you want to know more, actually there is an event coming up in November in not too far from here in Lisbon So the Peter, our CEO, also the person who announced PyScript a few months ago, he will be there. So you know go there if you are in Lisbon and you know you can talk to him and ask him more. So also at the end my job has gone but I've already talked about it many times. I've stickers so come to me to get stickers So I think probably that's it for today. Sorry about the short talk, but yeah,

talk to me and I will answer your questions. Thank you.