Python Bytes - #292 Thursday, it's always a Thursday
Episode Date: July 11, 2022Topics covered in this episode: rich-codex Pydastic 3 Things to Know Before Building with PyScript disnake Extras Joke See the full show notes for this episode on the website at pythonbytes.fm/2...92
Transcript
Discussion (0)
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 292, recorded July 7th, 2022. I'm Michael Kennedy.
And I am Brian Ocken.
Yeah, great to have you here, Brian. This episode is brought to you by us.
Check out Brian's book, and if you want a course, check out Talk Python Training.
Actually, a few more topics on that at the very end. Some cool stuff going on there I want to tell people about.
Right now, I would like to hear
about your first item. Something
rich, if you will.
Yeah. So it wouldn't be
our Python
Bites podcast if we didn't talk
about Will.
Will McCougan and his rich
project. But
anyway. Something
built from rich almost always yes yeah so uh will
uh tweeted out fantastic today fantastic project from phil ewells maybe um it generates screenshots
of commands on the fly from markdown files to keep them up to date and this is okay this is
great so like let's say you gotta uh you want to do a
like an output from a terminal output from your command or something in your readme it looks great
in a uh if it's like like here's a kitty cat i mean you're probably not gonna put that in you
but but but for instance uh you might have your help text and this looks beautiful um and and wow that can just show up so that's a
markdown file well yeah so that's just this you just stick this little line in your markdown file
and it uh runs it runs what rich codex does is oh yeah i forgot to say what it is the the project's
called rich codex um what it does is it looks for these image tags within your Markdown file.
And if there's backticks, it figures that you want to run that code.
And it runs the code and then puts the output in this image file.
And that's it.
And that's enough.
I see.
And then includes that in your Markdown as an image, right?
Well, it's already there.
It already shows up like this. It doesn't change your readme at all. It doesn't change your markdown as an image right well it's already there it's already shows up like this it
doesn't change your readme at all it doesn't change your markdown file it just generates the
image from from the code for you um and then i see then you have this so this is in this right here
so this is an image i'm sure generated from here and then you know you can resize it and it just
it's an image it's a vector image so it just resizes. How cool is that?
That is really cool.
So you can do these.
You can run a command like this,
just throw an image in there,
well, in your Markdown file or wherever.
You can also do code snippets.
So you can give it like this code snippet of,
I'm showing a snippet of some uh jason and it will um it'll detect this uh
this comment out of your code and then embed it as this this image and show up like this so uh
colorize your uh your code so the the one of the ideas around this is images are nice and they really make a readme really easy to, I mean, it's colorful, it's cool, and we like them.
But it's hard to keep up to date.
And this way you can have it kept up to date all the time.
What I really, really love about this is so you have all these things.
So you can do code snippets.
You can even do, um, uh, config files. Uh, you can have,
have if you want just to generate the image and you,
you're not referencing it from your read me,
maybe a reference to like somewhere else or something,
you can have it generated from a, like a config file.
But the cool thing about all of this is that he's got it set up for,
um, for GitHub Actions.
So you just stick this bit of code
within your GitHub Actions
and it'll regenerate your images for you.
It even does, if there's any changes,
it even does a commit, commits your changes back in.
So up to you.
That's pretty cool.
Yeah.
Yeah, yeah, that's fantastic.
Really sweet.
You check in your readme, the action runs
and then it rebuilds the graphics. Yes, exactly. Cool. And sweet. You check in your readme, the action runs, and then it rebuilds the graphics.
Yes, exactly.
And then you got a nice looking readme with just like this little tiny line of code in your readme.
Nice.
Pretty neat.
Yeah, that's a cool project.
Very nice.
All right.
Well, let's jump over to this one, which is sent in to us by Roman Wright. And it's actually a project by Rami Awar. And the
project is called, let's scroll down, get this right, Pydastic. So the idea is, it says, Rami
says, inspired by Sebastian Ramirez's sequel model and Roman the Wright's beanie. So sequel model and roman the rights beanie so equal model we've talked about that
that is hydantic models that are also sequel alchemy model so basically a database layer on
top of async that on sequel alchemy that you can do like async stuff and so on that is built on
hydantic and then beanie is the same thing built on top of motor, but the models are Pydantic models. So here's another one,
like an Elasticsearch ORM to do CRUD results and bulk operations and so on built on Pydantic,
but now talking to Elasticsearch. How cool is that? Oh, that is, that is very cool. Yeah.
Yeah. So if you were going to do an Elasticsearch and I feel like this is one of those tools that
I don't use, but I'm like, oh, I should probably learn this because it seems really useful.
So let's take a step back real quick.
So Elasticsearch is part of the Elastic stack, right?
There's the whole elastic.co company and so on.
But the core of this is the open source Elasticsearch, which is a distributed, restful search and
analytics engine capable of a whole bunch of different things uh stores your
data for lasting lightning fast search fine-tune relevancy you know ranking of your search and and
so on and basically search and analytics right so if you want to do that but you want to have
your models be cool python pydantic models well this pydastic thing is the way to go. Nice. Kind of fun name too.
So it does.
These all have fun names.
So the way you do it is, you know, if you know Pydantic, you basically know how to do this.
You create a class and it derives from now an ES model, but you can bet that that's also
like a derived class from a Pydantic base model, model base.
I think it's model base.
Anyway, the base class of Pydantic.
And then you just say like name,
stir, phone, optional, stir.
And even you can do the Pydantic field stuff.
So you could say it's a date time,
but it's a field with a default factory
of datetime.now, right?
All the cool stuff that you would do
for a Pydantic model.
And then you can add a meta class
to add information over to, um, elastic.
So for example, the meta here says index is user.
Then you just set up a connection.
You create one of these objects and you save it.
Uh, you can change its name and save again, you know, standard ORM stuff.
You can get a, one of these objects by ID and, um, yeah, you can even create
new ones, delete ones, pretty straightforward.
The other thing to realize
is it uses the unit of work design pattern.
So it says,
this is based on SQL Alchemy session,
which is the unit of work applied to SQL Alchemy.
So, you know, by the transitive property.
Anyway, you create a context manager
and then you do a bunch of work
and then you commit it.
So you can transactionally make changes
to your data in Elasticsearch. Oh, that's cool oh that's cool yeah i like that yeah and that's pretty much
it right you can query you know what there is one uh i feel like this is a little bit ironic
uh the one part missing still haven't got an idea how to wrap the underlying api productively so
um how do you search elastic search i'm not really sure yet but you could put something to it
i love that it just says that it just says in the docs still haven't got an idea of how to wrap the How do you search Elasticsearch? I'm not really sure yet, but you could put something to it.
I love that it just says that.
It just says in the docs, still haven't got an idea of how to wrap the underlying API.
Anyone got an idea?
So shoot me a thought if you've got some ideas out there.
Maybe you all can figure it out.
So I guess when you do a regular query, you get these results back.
Maybe it's a dictionary.
I haven't actually tried the raw API for Elasticsearch either, but if you get a dictionary back or list of dictionaries,
then you can just jam those into your Prydantic models anyway, right? You can transform them back,
but yeah. Anyway, if you're doing Elasticsearch stuff, you know, check this out. It looks pretty cool. And if you have ideas on how to address that, the final bit to do the search in a good
way, then, you know, maybe take up that conversation yeah nice yeah cool um well i'm going to stick with kind of some web stuff for a minute
and um and talk about py script again so i we've covered by script several times i think and uh
so but i ran across this article called the three things to know before building py script and you
gotta say one of the I haven't played,
you've made a little application with PyScript already, right?
Yes, I have, and I loved it.
Yeah.
It's very cool.
So I hadn't played with it yet,
and one of the things I love about this
is it's got a little crash course,
and it's really short.
The crash course is,
you gotta stick this in the head,
and that's it.
And then in the body,
you wrap some of your Python code in PyScript
and when you print things, it comes out.
That's pretty much it.
Crash course.
That's pretty easy.
One thing I would like to throw in there
about the script bit,
it's not exactly obvious in a lot of the documentation,
but you can host your own runtime
of the PyOA diet host your own runtime of the pi
oedide and piescript web assemblies and stuff okay so this just downloads it off of there which i
don't think the caching works super well from there but you can put it in your own place and
and cache it and so on okay so so i think that's the pi env or something so people are checking
that out and you maybe want to pin it so it's stable or something like that then you can you can do that or just make sure it's on your server so
that you're not pulling it off of their server all the time or something right because maybe
they'll change it or who knows right exactly yeah and that i was i was curious about that but i was
uh for when when you're just learning and playing around with it you can obviously just drop drop
the the script link in there yep yeah so if you
just a few uh snippets and i forgot to mention who wrote this uh brandon brig brandon riggs
uh wrote this and uh one the first thing that he brings up is uh package indentation matters and
i'm glad i probably would have messed this up so this is kind of interesting. You can do a high ENV and in one of those tags, HTML tags.
But the packages have to be, have dashes and they have to be all the way to the left.
So you can't have those indented with your ENV.
Yeah, it's nuts because it's basically embedded YAML.
Oh, okay.
And YAML is sensitive to indentation,
but white space is not supposed to matter in HTML.
So if you do auto reformat of the doc,
it keeps breaking it.
So you got to be super careful with that section.
Yeah, it's a pain.
So for something to watch out for,
the next one is local file access,
which I'm like, I don't believe this.
And I had to try it out and play with it.
Because normally you don't access local files
through JavaScript,
but you can apparently through this Python thing, sort of.
So you have to set up a paths in your environment.
And then I guess, what does the slash mean?
Just the current directory or something like that?
Or I don't know, slash means.
Yeah, I think what it means, those have to be static files on your web server. mean just the current directory or something like that or i don't know yeah i think it's um i think
what it means those have to be static files on your web server so this says for people listening
forward slash views.csv you need to be able to take that url and jam it into the just a web
browser and see the file so if it was like your server.com slash views.csv that's the little
fragment without the domain that goes there yeah and in this part when i was trying it just on my own but the first part actually you can you can run a little uh py
script thing just as a file an html file on your computer but if you're doing this the the local
file thing it needs a server so you need to run a little but there's a there's ways to do still a
little python server um easy um so that that's interesting uh the in his examples is using
using a csv file here to just um like uh import pandas and use pandas to read a csv file which is
pretty sweet um and i yeah uh the last bit is dom manipulation which i never which of course you'd
want to do within if you're going to stick some python code in your in your in
your pi script but um there's just some gotchas that he can't ran across or just some things that
weren't obvious to him so that um i guess it's a there's a there for buttons there's a pisys on
click um or pys dash on click and then there's a how you get the element you can get an element through the
document and and then you can put it back you can you can do py script right and right to an element
and there's it's nice just to have this little example to be able to see so i uh it's good to
try this out so like on this um we're py script right it's going to this uh uh out out p well that shows up as an id and a div
somewhere so you can grab try to specify where you want to write to and then uh yeah and then
how to get this this kind of example is kind of nice because it has like the grabbing from
grabbing from an input field and how to deal with the click and stuff like that so yeah that's really
nice um and it can you go back
really quick to where you have the uh the e the pi emv section and the static file the csv yeah
a little bit further a little further back a little further so one of the things that's really
cool is you can actually in addition to these csv files you can put python files in there
yeah i was curious about that one to the other you can say, if you had like a helper.py, you could say somewhere
else, import helper, and then call helper.such and such or whatever.
And that's all you got to do to kind of like register where the modules live.
Okay.
So you can break your code up and you don't have, the example here shows it all written
in the HTML file, which is kind of insane.
And you should put those into separate files.
You wouldn't do that with JavaScript generally.
Right.
So you shouldn't do it with Python either, right?
So it's really easy to use that path thing
to break it up across Python files
you get full autocomplete and everything with.
And then same thing then,
would your Python file have to be served
so you could just stick it on a...
Yes.
Yeah, just serve it.
Yeah.
So that part, the client side,
it's so weird to say,
the client side Python files have to live
in a static servable section okay normally that's not possible normally that's blocked as it should
be yeah nice anyway uh interesting um this was enough to to get me to try it uh to try py script
because it was pretty fun so that's pretty cool yeah it's very fun and it's it's quite neat uh
all the things you can build.
And I think we're just at the beginning, right?
A lot of the examples are data science oriented,
but some of the things you showed,
like the events and the DOM manipulation and whatnot,
you can build full-fledged JavaScript run-in style apps.
They don't have to be only showing graphs
and data frames, you know?
Right, and I think we're going to see some
interesting stuff because like you said there um if you serve up your uh like an extra helper file
or it in basically it might be most of your code actually is in in these extra files um then then
we really can i mean it pulls some some do most of your code and do demos and it's all live and
everything and it'll be
pretty cool it's gonna be fantastic now when i tried this um the delay actually surprised me
it shouldn't surprise me because it's pulling in the whole whatever the py script library and
everything but it does like this little spinny wheel thing and you gotta wait a few seconds
now for a lot of applicants and i think that's one of the reasons why i think data sciences could
really take off quickly is because they're not going to, it's an internal thing. You're not
bothered by it. It's not something I would, I don't think it's, I'd like it to be the point
where you could, you could really use Python instead of JavaScript in, in a customer application
or something, but I don't think it's going to be a while. Yeah. It's going to be a while before
you see it in like a landing page scenario, but you may well see it in the equivalent of gmail but internal you know like that kind of app where
you open it and you stay on there for a while yeah it's true especially if it's an internal app yeah
some back-end stuff yeah sure yeah after you know i gotta log into this thing and i sit and then like
so the video that i did last month or so was about how do you host that and then do a progressive
web app so that it stores that offline.
In which case the web app startup time is like one second.
Always.
Like it's really, really quick.
If you get it off of the server, like they list here, then it takes a lot longer.
So anyway.
Cool.
We'll have to try that next.
Yeah.
I'm super excited about this.
This is great.
All right.
What do I have next?
Well, let's see.
Okay.
We covered these. Next up great. All right. What do I have next? Well, let's see. Okay. We covered these
next up more fun names. This one comes to us from Sean Koenig and it says, Hey, this might be a good
one. The, uh, you like punny names. So this is dis snake, D I S snake, this snake and dis snake.
So dis snake is a modern, I agree with that, but you'll see it in a second, easy to use, feature rich, async API wrapper in Python for Discord.
Okay.
So Discord being the community place, there's a lot of stuff happening there.
It's a little, fills a slightly different role than Slack and Teams, but more community oriented rather than company oriented, I suppose.
Although you can sort of do that with Slack. It's not really like it's focused. It's more some of the people jammed
it into. Like for example, if you want to save the history of your community and you're doing Slack,
you've got to pay per user. But it's like, if it's a public community, like everyone who randomly
shows up, you're like $8 a month for them too. Oh no. Right. So for lots of reasons for gaming
and others, Discord is very popular, but what you can do is you can create bots for your community that
do fun things and this is a way to do that fun and easily in python so features include a modern
async and await api one of the problems apparently you run into is you can overrun your rate limit by being too chatty.
So it has built in rate limiting so it doesn't destroy.
It doesn't get 409 too many requests errors.
It has a really cool command extension.
You'll see in a moment, object oriented and both optimized for speed and memory.
OK, so that's pretty cool.
Probably the best way to understand is to just check out the quick start.
So here's a minimal bot.
Here's what you do, Brian.
You import Disnake and you create a client
with a Disnake.client.
And then you create some functions
and you decorate them.
And then you say client.run
and you give your API token.
So then you just write regular async functions.
It handles the execution of the async functions,
but they need to be async. And you just say,
here's an event for onReady.
Here's an event onMessage.
So if somebody sends a message to
that community, then it's going to
call back into here straight away.
Alright, so just this function, this
onMessage function gets called every time
a message happens, including if
the thing itself sends a message, or
the account itself. So it sort
of checks to make sure it doesn't go into like some sort of infinite loop where it sends out
messages if you send a message, but it just checks, hey, if you send the string dollar hello,
it'll send back hello. The dollar means send this to the bot. You know what I mean?
Okay.
In this world. So yeah, that's pretty much how it works. It's kind of interesting,
but if you look at
the commands then it gets way more powerful so the commands are what people would probably think of
like what i want to do with a bot so with this minimal bot what you do is you sync the events
like on message and ready and such but with the commands you can uh go over here to the import
the bot thing and say at decorate another async function, at bot decorate command. And this
one's called foo. So when that's the case, you know, it takes arguments and so on. In Discord,
you can write $foo and then other stuff and that other stuff goes to the argument. So all you got
to do is have a single function with that name and you put the decorator on it. And now all of a
sudden that's a command in Discord that talks to the bot. That's cool. Yeah. Isn't that easy?
Yeah.
Yeah.
So there's some other examples.
You can register your bot with a prefix.
So you could use, I don't know, exclamation versus dollar versus whatever it is you want to do.
And by default, the command name is the same as the function name.
But you can also pass in a name.
Like, for example, if the name is list you can't have
a function called list because it'll freak out what list means so you can give it a name and
then use a different function name and so on and then again pass in arguments and such so there's
some cool little examples of they're saying dollar test hello and the documentation bot says hello
and they say dollar test hello world hello world so Hello, world. So. Yeah. Okay. So I was thinking stuff like you could give, give people access to some of these commands.
Like if you want to subscribe to my newsletter, you could give it a, a subscribe command with
your email address and subscribe yourself or something.
Yeah.
You could totally do that.
And you could just on, on the bot side of things, you have that command.
It takes an argument, which is the email.
And then you just use the MailChimp or whatever
API to just stick them in there.
Yeah.
Yeah.
And that's running on your server.
So it can do,
you know, all sorts of things.
Interesting.
Yeah.
Cool.
So if you want to do
something with Discord,
check this out.
I haven't used it
because I haven't tried
to build a bot for Discord,
but this looks pretty fun.
So go check that out.
Definitely.
Yeah.
So that's our items for today.
Do you have any extras for us?
I have extras.
I do.
I do have extras.
And I actually came up with another extra
while I was thinking about the extras.
So yeah.
Okay.
So some exciting stuff.
Let me pull my screen back up here.
What's the order I want?
Let's talk about this one first.
So last time I spoke about the modern APIs with FastAPI, MongoDB, Beanie, Python as a in-person live course that runs for two weeks
that I'm teaching and people can check that out. Starts on August 8th. So I'll link to that,
but that's not my item because that's what I talked about last time. What I decided to do is
I'm opening up a scholarship program for people who are underrepresented in the Python
space and programming in general, and who maybe also just need a hand up through different groups
there. So if you're a woman in tech or a person of color, or maybe you lost your job and you're
like, I'm trying to get into programming, maybe this course will help me do that. I try to get
into programming as a job. Check out the scholarship link that I put in here. And I'm saving some spots in that live course
for people who could benefit from that.
Oh, that's pretty cool.
Kudos.
Nice.
Yeah, thanks.
Yeah, thanks.
Okay, now, just this is fresh, hot off the presses,
as in is one hour and 30 minutes old.
We have Operation Python 2022 software bundle
from Humble Bundle.
So we're doing another Humble Bundle this year.
And I've got my Git course and a Coder course and a Pythonic code course that I put in there for people to take.
But there's also 24 other topics, like some stuff from RealPython, some stuff from Roof and Learner, from PyCharm, and so on.
So people can look through there and check that out.
It's certainly a good way to get a deal
on Python educational stuff.
Nice.
Cool.
That's pretty cool.
All right, and here's the one that,
yeah, thanks.
I'm pretty excited about that.
The last one is,
I told you about these crazy headphones that I have,
and I just want to tell people maybe quick about this.
So I got this thing called Ultimate Ear Fits,
UE Fits,
and they're these fitted earplugs, earbuds, which I'm wearing now, obviously on the YouTube channel. And what they
do is they come really soft and smushy and they're light sensitive and heat sensitive. You put them
in your ear and you press a, you pair them to your phone, you press a button, they start blazing out
light and heat and they form to the shapes of your ears. So, Brian, what do you think of that?
That's, I don't know.
It's unusual, right?
Yeah, it sounds pretty cool.
I just don't want it to go too far and take over my brain.
What if it's too mushy and it, yes, if it gets in there, you don't know.
You can't get it out.
No, it's probably safe. Anyway, you and I were talking about that right before we started.
So I thought I'd just give a quick shout out to that.
So how's it sound so far?
It sounds great.
It's kind of like you're wearing earplugs.
So the sound is a little bit blocked out,
but then there's a speaker on the inside.
Okay, nice.
Yeah.
Any extras from you?
I don't have any extras.
All right.
Well, let's tell the joke then.
Okay.
So this one is a proposed,
more of a comment on a proposed idea,
which gosh, it probably will never happen,
but I would wish it would.
It says, if we're going to unionize and forget wage, increased wages and that kind of stuff,
I want this instead as a software developer.
And what it is, is it's a proposed new standard year, which breaks up the year into 13 different
months.
Yeah.
They're all 28 days, which almost exactly rounds out the year. So every single month,
the first is Sunday. The fifth is Thursday. The 26th is a Thursday. It's always the same
year after year after year. So you know about the days, you know, how many months it is,
how long, uh, if you have 28 days or, you know, 45 days from now, you know,
doesn't matter what month you're in and how
far that pushes you into the next month.
But there's one day left over.
It says the one day left over is New Year's Day.
It isn't a weekend or a weekday.
It's magic.
And on leap years, it's a double vacation day.
The rest is easy.
Someone says, I'll see you on the 19th.
You know, it's on a Thursday.
Doesn't matter what month or anything.
What do you think of this?
I've always been a proponent to the fixed calendar.
I think it's a great idea.
And I don't remember the history, but I'm pretty sure that we probably started out this way because it's the lunar calendar, essentially.
Yeah.
Yeah, pretty much.
Yeah.
And I thought I read somewhere once that people were freaked out by the number 13, so they dropped it to 12 or something.
I'm sure somebody in the chat or somebody will correct us.
I think we could fix it if you just started with zero-based.
Go zero to 12 or 13.
That's better.
Yeah.
January's zero.
Perfect.
Exactly.
Yeah.
Yeah.
Anyway, it's sort of a joke but the comment about it that's the joke
yeah like you got any other jokes uh you want to share is that uh no i don't but i didn't look
all right fantastic good talking to you again well yeah it's great to talk to you as always
thanks for being here and thanks everyone for being on the audience yeah thank you bye yeah
cheers bye