Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Follow along with Treehouse Teacher, Kenneth Love, as he plays around with APIStar to create a single page web app.
-
0:00
[MUSIC]
-
0:05
This week we're gonna do something brand spanking new.
-
0:11
I don't know how many of you all know the name Tom Christie, if I just say his name.
-
0:15
But Tom Christie is the person who created Jengo REST framework.
-
0:22
Which we have used on this stream before,
-
0:24
I'm sure you have all heard of Django REST framework.
-
0:27
So he got it into his head a little while back to do a new API framework
-
0:33
without having to be built on top of Django or Flask or anything like that.
-
0:39
He wanted it to be just an API framework,
-
0:41
just straight framework doing whatever he wanted.
-
0:44
And so he created this one called API Star.
-
0:49
And I haven't really gotten to play with it much yet.
-
0:52
But I talked to Tom a little bit at PyCon
-
0:57
last week and I wanna just give it a try.
-
1:02
So that's what we're gonna do.
-
1:04
We're gonna just give it a try.
-
1:05
So we are going to do this, still haven't figure
-
1:11
out how to do this quickly and safely through,
-
1:18
The PyCharm, that's what this thing is called.
-
1:22
And then we'll see what we're getting ourselves into cuz,
-
1:28
like I said, I haven't played with this at all.
-
1:33
I have no clue what's going on in here.
-
1:37
So we're gonna just try it out, see what it's like.
-
1:40
And then, if we can get it working well enough, I'm gonna try and
-
1:43
do some sort of little like single page web app kind of thing with it.
-
1:47
I want to play with Vue.js which I have also not ever used.
-
1:54
So, between those two we'll
-
2:00
end up with two things I have never used and hopefully something that works.
-
2:05
So let me slide this over here a little bit, let's pop open this terminal and
-
2:09
let's see if we can generate this thing.
-
2:12
So we'll do apistar, no, we have to install apistar.
-
2:16
That would be handy first.
-
2:18
All right, Settings.
-
2:21
Not Version Control.
-
2:22
Project Interpreter, and it's at a plus sign and let's look for apistar.
-
2:29
There it is, we'll go ahead and install that.
-
2:32
And we are on version 0.1.17.
-
2:36
So if you're wanting to follow along that's the version to install.
-
2:41
It was [INAUDIBLE] the newest one.
-
2:44
And as far as I know this is all we have to have to start.
-
2:48
So we're gonna start doing just a plain static hardcoded JSON API,
-
2:54
not pulling from a database or anything like that.
-
2:59
And then once we get it going and
-
3:01
working out then we'll see what we can do as far as a database back end.
-
3:05
I talked to Tom a little bit last night and he said there were,
-
3:12
There were a few things that weren't working great with models, so
-
3:17
we're gonna just leave those alone for now.
-
3:22
All right, so if we look here we now have an app.py and we have a tests.py.
-
3:28
So a test.py is kind of cool.
-
3:31
We have this data welcome.
-
3:34
We pull in welcome from the app so here's welcome and
-
3:39
let's see, I need to make this way smaller so
-
3:43
that I can see your chat and I can see this part.
-
3:48
All right, cool.
-
3:48
So you can see that we have this welcome function
-
3:53
here where it takes a name that's defaulting to None.
-
3:57
If the name is None then it just comes back with welcome to API Star.
-
4:00
Otherwise we come back with welcome to API Star and whatever name somebody put in.
-
4:05
And we're putting this into a message key in a dict.
-
4:10
So then we have some routes.
-
4:12
So our first route is forward slash, which that gets welcome.
-
4:15
And then we include the docs routes and the static routes at docs and static.
-
4:20
And then we make our app, and we set the routes equal to the routes.
-
4:23
Okay, that seems fairly straightforward.
-
4:27
I don't see anything there that I'm like immediately lost on.
-
4:33
And then let's check out the tests, so we test welcome, so
-
4:37
we have data where we just call the welcome function.
-
4:41
And then we assert that data is welcome to API, is the dict with welcome to API Star
-
4:45
which, since we didn't pass in a name is what we would get, and then we test it.
-
4:50
So we should be able to do, take this, paste the new one in,
-
4:53
[SOUND] and we should be able to do test welcome name.
-
4:55
And we should be able to do like test_welcome_name,
-
5:01
and we could say name = Kenneth, and
-
5:04
this should be Welcome to API Star, Kenneth.
-
5:09
All right, yeah, making sure I was spelling all of that right.
-
5:13
So then if we were to run our unit tests.
-
5:19
That's true, those are not unit tests.
-
5:23
How do we run those tests?
-
5:26
We just run API Star tests.
-
5:29
Okay.
-
5:40
Python?
-
5:42
Custom.
-
5:46
No, wait, wait. We don't want that as Python tests.
-
5:49
We want, to just run,
-
6:07
You know what, we're just gonna do it in here.
-
6:10
apistar test.
-
6:13
There we go, so we ran our three tests and we're good.
-
6:18
So it looks like that's using py test.
-
6:20
So I can probably do edit configs,
-
6:26
add a py.test.
-
6:35
Let's see if that one works.
-
6:40
[SOUND], whatever, we'll figure that out later.
-
6:42
All right, so haxmat wants me to do something with Go.
-
6:49
If I knew anything at all about Go I would but I don't.
-
6:53
I don't know anything about Go and this video will end up on YouTube.
-
6:57
You'll be able to watch the whole thing on YouTube later on.
-
7:01
And I don't know about it being on the Treehouse site.
-
7:04
We were talking about doing something there, but
-
7:06
it's definitely gonna be on YouTube.
-
7:08
Okay, and so let's look at this other test here which is,
-
7:11
this is the http_request test.
-
7:13
So we make a test client just like we would do in Django.
-
7:17
We try to get local hosts.
-
7:18
We make sure it comes back with a 200 and
-
7:21
we make sure it comes back with Welcome API Star.
-
7:24
All right, so that is pretty cool.
-
7:27
You all feel like you get an idea of how this is done and how this is done?
-
7:34
If you feel like you're still lost let me know, but I'm gonna go ahead and
-
7:38
I'm gonna try to run this thing.
-
7:39
I didn't wanna open that image.
-
7:42
So I wann do apistar run and
-
7:45
then, What happened here?
-
7:53
Non UTF-8 code starting with something there.
-
8:00
No encoding.
-
8:07
Interesting.
-
8:09
What if I just did like python app.py?
-
8:14
No.
-
8:26
Okay, let's see
-
8:30
what's going on here.
-
8:36
Learn Go like I did Rust.
-
8:38
You know what, I might, not today.
-
8:46
Not today, but maybe I will.
-
8:49
We'll find out.
-
8:53
Yeah, what happened there?
-
8:56
Let's just search for Windows.
-
9:00
No, nothing.
-
9:04
Non UTF-8.
-
9:07
No, weird.
-
9:14
Okay, what's up, Tom?
-
9:17
This thing should be, this thing should be working.
-
9:26
Weird, okay, I'm gonna try this in a different shell.
-
9:32
Let's try this not inside the PyCharm Shell,
-
9:39
because sometimes the PyCharm Shell is iffy.
-
9:46
Sometimes it just doesn't do the things I want it to do.
-
9:48
Are we good?
-
9:49
Okay, api, no wait what do we call this?
-
9:53
game_api, that was it and then I need to
-
9:58
do .Virtualenvs Is game API
-
10:03
scripts activate.ps1.
-
10:09
And then API star run.
-
10:10
[SOUND] Got that again.
-
10:15
All right, let's grab this and see what we've got.
-
10:24
Alright, while vaka two,
-
10:28
yeah we're definitely using python
-
10:33
today, I can't go edit an exe,
-
10:38
that's not how exes work.
-
10:44
Okay.
-
10:53
Apistar run non UTF-8.
-
11:00
Isn't it great?
-
11:04
When we hit these little things, don't you all love this?
-
11:09
And see, yeah, APIStar is pretty new.
-
11:11
So we probably aren't gonna find a thing.
-
11:22
[MUSIC]
-
11:39
I saw some people talking about Hug yesterday, so
-
11:41
I want to check that one out too, just in case.
-
11:43
Maybe we do hug instead because we're having issues with, we
-
11:53
can't we're having issues getting this to run.
-
11:57
Hopefully Tom is watching cuz I was talking about this yesterday and
-
12:02
Tom will be like, "Hey dude, here's what you're doing wrong.
-
12:07
Any luck?
-
12:08
You there Tom?
-
12:15
Yes, something is weird here.
-
12:19
Because it doesn't run.
-
12:24
[MUSIC]
-
12:28
Hi Py three support.
-
12:30
[MUSIC]
-
12:35
Sorry for just having to look for this everybody.
-
12:37
But that's what happens.
-
12:47
You're gonna go on a rant about how great go is.
-
12:50
I'd really rather you not.
-
12:51
I mean go is fine.
-
12:59
But that doesn't mean that I wanna listen to a rant about it.
-
13:04
Game_api/Scripts.
-
13:09
Okay.
-
13:09
Can I cat api/star.exe?
-
13:13
Right.
-
13:14
That's not like I can go and edit that.
-
13:27
What was it that I did the other day?
-
13:28
It was open
-
13:32
Atem APIstart exe.
-
13:39
No? Okay.
-
13:42
Whatever.
-
13:43
Let's go here, and
-
13:46
let's go here and here and
-
13:51
here and here and here and here and
-
13:58
I want to open with that.
-
14:02
Sure let's open that with code.
-
14:05
Invite Tom to co-stream, yeah I might could.
-
14:07
It is a binary, but that doesn't mean I don't want you to open it.
-
14:13
I still want you to open it.
-
14:23
Can we drop it into here?
-
14:30
No.
-
14:35
Edit with VIM, this better not be running it in DOS mode,
-
14:40
yeah, see that does me no good, okay.
-
14:44
So How do we fix that?
-
14:51
Let's go look at what we're doing when we install this.
-
15:04
Okay, api star.coi main.
-
15:26
Main, so we set up Python Path, we get the app path.
-
15:28
App = App, and then we try to do App.click.
-
15:35
So he's using click somewhere?
-
15:43
Come on, I should be able to do API star, if I can do API star test
-
16:02
I can do new.
-
16:03
I can run the test suite.
-
16:12
Why doesn't it run?
-
16:18
Line 1, non-UTF-8 code starting with \x90.
-
16:29
Python 3.6.
-
16:32
Okay, so let's see path to six three,
-
16:39
non UTF 8 binary are executable,
-
16:44
let's see if we can solve this?
-
16:51
Maybe there's a way for us to fix this, everybody.
-
16:56
And if there's not then
-
17:01
we'll try something different.
-
17:06
Not a Python script so python will just run it as that.
-
17:09
[MUSIC]
-
17:14
Yeah, that's what I was doing, but,
-
17:22
Okay, let's just try it like this.
-
17:26
C/users.kennan.virtualenvs.
-
17:31
Game API, scripts API star dot EXE.
-
17:38
Run.
-
17:40
Nope same thing.
-
17:41
Alright, so that didn't work.
-
17:47
Why is it trying to run it like python though.
-
17:49
Like I'm getting obviously a python
-
17:51
[MUSIC]
-
17:54
Error here.
-
17:55
[MUSIC]
-
18:25
What I really wish I could do is just do Python.
-
18:30
Apistar app.py.
-
18:37
And the module name, api star main, api star as a package cannot be,
-
18:42
okay, so what about just app.py?
-
18:44
That doesn't do anything.
-
18:50
That has no idea what I'm talking about.
-
18:52
[MUSIC]
-
19:30
And that does exactly that.
-
19:34
Tom, Tom, Tom, Tom, Tom.
-
19:39
You know what? Let's try this.
-
19:45
Tom https://twitter.com/tomchristie.
-
19:52
I think that's it. There may be an underscore in there.
-
19:53
Yep, that's not him.
-
19:54
I think there's an underscore right there.
-
19:58
That's him!
-
20:02
Let's send them a message, I'm getting an error about
-
20:09
non ETF 8,
-
20:13
code NV API, start of EXE, any ideas?
-
20:19
You're live on stream.
-
20:22
All right.
-
20:23
Let's see if he says anything.
-
20:24
[LAUGH] All right but you know what?
-
20:30
If he doesn't we'll give him a couple of minutes to answer and if he doesn't then
-
20:47
Yeah, adding an issue pause would make the most sense,
-
20:53
but I wanna interrupt his day [LAUGH].
-
20:57
To some degree.
-
20:58
I don't think an issue would be a bad thing.
-
21:02
What I'm interested in though, is how it's actually building the things.
-
21:08
So.
-
21:13
Publish.
-
21:16
That's not doing what I want.
-
21:17
That's just him publishing his thing, okay.
-
21:21
So what we're actually wanting is what's inside API star and then command.
-
21:29
What's in here?
-
21:33
Run run run run run, from APSR get current app, app equals get current app.
-
21:38
Now it's running for me click that echo starting up.
-
21:41
So then he does this run simple from verkzug.
-
21:50
Almost wonder if I could fake that.
-
21:57
I shouldn't have to fake that though.
-
21:58
All right let's try this.
-
22:00
Let's try doing python, import apistar
-
22:09
apistar.commands.
-
22:17
Run.run simple?
-
22:21
Nope, dot run.
-
22:31
Okay, so we need to do local host.
-
22:36
And we need to do 80 80.
-
22:39
An integer is required.
-
22:41
Okay.
-
22:45
Can't find main module in blank.
-
22:50
That's true.
-
23:02
Also, let's open up the discussion page here.
-
23:16
So that's if we wanted to drop down the WSGI,
-
23:17
which we don't want to at the moment.
-
23:22
So that's the development server which we're already having problems with.
-
23:27
So that's not what we want.
-
23:33
All right, so what I'm wondering is if we could
-
23:39
up here do something like from api star.Commands import run.
-
23:50
And if we go back and look at what run was doing.
-
24:00
Where were we looking at that?
-
24:03
Welcome to hacking somebody else's software, y'all.
-
24:06
Not normally what I would be doing, but
-
24:09
I don't necessarily feel like I have something else I can do.
-
24:14
Okay.
-
24:16
So, then okay,
-
24:21
no this is pie charm.
-
24:26
Andy okay.
-
24:34
So then could we do, wait wait wait.
-
24:38
We have an app.
-
24:39
Sorry, let me hop over here and look at this thing.
-
24:43
So we have an app, right?
-
24:45
So we imported app, I'm assuming we have an app right there, we do.
-
24:50
Does an app have a run command?
-
24:53
Browse commands What can I do there?
-
25:02
[SOUND] Python.
-
25:06
We'll do the Python consul here.
-
25:09
There we go, import app.
-
25:13
App.app.run no commands.
-
25:19
Function new, function okay, app.app.run.
-
25:24
App object.commands.run.
-
25:51
Aw!
-
25:53
Wait, wait, wait.
-
25:54
Let me, that is not what I meant to do.
-
25:59
Import app.
-
26:01
Okay and then here, let's do local host.
-
26:08
And then local host.
-
26:13
Aw, man.
-
26:14
[LAUGH] Thought I was right,
-
26:19
thought I was gonna be right, I was not right.
-
26:24
Tom still hasn't responded which that's cool.
-
26:27
He's got stuff to do and he's in like the UK too anyway.
-
26:31
So I'm guessing
-
26:39
That doing that one probably doesn't do what I wanted.
-
26:45
Search Windows, no results found.
-
26:53
Really?
-
26:55
UTF8 No results found,
-
27:01
non UTF-8.
-
27:04
No results found.
-
27:08
Why would it fail for us, but not be failing for anybody else?
-
27:22
Anybody wanna wager a guess on that one?
-
27:31
Let's just search for win.
-
27:34
Name API Star Logging Serverless deployment, no.
-
27:37
Okay so, We can do those commands.
-
27:47
And we get a thing here but
-
27:49
we don't really get the thing that we're looking for.
-
27:53
Just got a buzz on my phone.
-
27:56
Tom?
-
27:56
No, it doesn't know what we're talking about.
-
28:01
Fun, no none.
-
28:02
Let's try reinstalling.
-
28:07
So let's go here, API star.
-
28:11
Let's delete API star.
-
28:15
And, let's reinstall api star.
-
28:23
Yeah, pause, it could be that they're using, that they're not using Windows.
-
28:40
Right, no such option to clean.
-
28:44
That's so weird.
-
28:45
Non UTF-8 code, starting with /x90
-
28:50
on line 1 but no encoding declared.
-
29:01
Okay,
-
29:17
I wonder if it would be possible to do, what kind of scripts do we get?
-
29:25
If any.
-
29:32
And I'm not gonna work on this too much longer because if we're
-
29:37
not getting anything, then we're not getting anything.
-
29:44
Those are his scripts setup.py.
-
29:47
Okay, so if we look in here there is a little substance installed,
-
29:54
we had entry points, api*.cli.main
-
30:14
Yeah.
-
30:21
All right, well I guess we will try something different then.
-
30:29
It's kinda disappointing, I've been having a lot of streams lately
-
30:33
where we get three things and we can't finish something.
-
30:40
Which is really too bad.
-
30:41
Let's try out hug Let's try out this hug thing.
-
30:51
So we'll still do this as our game API thing.
-
30:54
But we will install hug instead
-
30:59
of I mean yeah, we need Pip.
-
31:04
We need setup tools.
-
31:05
We don't need any of the rest of those.
-
31:08
Let's just try reinstalling everything.
-
31:17
And let's just see what happens.
-
31:18
I don't think that's going to matter at all.
-
31:26
Yeah.
-
31:29
Let's try it one more time.
-
31:31
Just one more go, cuz I'd love to see this work.
-
31:36
Also, I could try a different terminal, not that I think it would matter.
-
31:43
Or get bash.
-
32:00
All right, into there, okay.
-
32:02
So then source.
-
32:03
.virtualenvs/game_api/Scripts/activate.
-
32:18
Ps1 no?
-
32:24
No, okay.
-
32:30
Game_api/scripts/apistar,
-
32:40
What's this apistar script?
-
32:47
Let's see what this thing is.
-
32:52
Okay, first of all though,
-
32:56
.virtualENVs/scripts, Nope,
-
33:04
game_api/script, activate.ps1?
-
33:07
No, .bat?
-
33:15
I don't want you to open that file.
-
33:17
There we go.
-
33:21
apistar does that work?
-
33:24
It does, okay.
-
33:26
F: Projects game_api, dir apistar run.
-
33:32
Still gonna get that same thing, okay.
-
33:38
So that's fine I guess.
-
33:42
If I come over here and
-
33:50
I close all this and
-
33:56
I hop into here, no.
-
34:14
Scripts\ and that was apistar-script.py.
-
34:26
Can't open that.
-
34:27
Yeah, cuz you're Windows, okay.
-
34:35
Okay, run.
-
34:44
Ugh, [LAUGH]
-
34:50
so close.
-
34:55
Let's try opening up that file, though.
-
34:56
Let's see if we can do that.
-
34:59
So I'm gonna go there and
-
35:02
then I wanna go into virtualenvs, game api scripts.
-
35:07
API let's try that one.
-
35:15
That is definitely not a thing, so let's do the thing we gotta do.
-
35:23
Open that.
-
35:26
Hey Maurine, how are you doing?
-
35:36
That all looks like UTF-8 though.
-
35:37
What's not UTF-8 there?
-
35:44
It's not the bang pound, right?
-
35:48
That makes no sense.
-
36:05
Let's try
-
36:10
the Latin one.
-
36:17
That's all ASCII, it's definitely all ASCII.
-
36:19
Let's stick that in.
-
36:24
Okay.
-
36:28
Okay.
-
36:32
Of course that doesn't do it, because, you know.
-
36:46
What is \x90?
-
36:48
Hex?
-
36:53
Device control string?
-
37:10
Device control string.
-
37:15
That makes sense.
-
37:32
So with the bold dollar sign.
-
37:39
Hey Nev Miguel, these come up weekly.
-
37:41
Not every week is me, but it is weekly, yeah.
-
37:47
Pause, what's up with that bold dollar sign?
-
37:51
I am not seeing a bold, that one there?
-
37:55
That's just syntax highlighting for,
-
38:01
For this.
-
38:05
Let's.
-
38:27
Yeah, display all those.
-
38:28
Okay, python.exe, does that work?
-
38:32
Does that do anything?
-
38:39
No, what about pythonw?
-
38:44
No.
-
38:48
Okay, source,
-
38:53
virtualenvs/game_api Scripts.
-
39:07
I don't have a way to do activate in here.
-
39:14
Okay what if I don't source this?
-
39:17
How do I get,
-
39:28
Right, that's not gonna work.
-
39:29
Python -m venv venv let's just do this.
-
39:35
Let's do this the old school way that I know to do this.
-
39:38
See if it works.
-
39:45
Currently not working.
-
39:48
Okay, there we go.
-
39:50
Did we get?
-
39:51
We did source venv/bin,
-
40:03
[MUSIC]
-
40:16
Maybe encode the string in UTF-8,
-
40:18
is PyCharm using your system default encoding?
-
40:22
Marine, we're getting the same thing whether I run it in
-
40:26
any of a random number of terminals or what.
-
40:30
So I don't think it's,
-
40:32
[MUSIC]
-
40:35
I don't think it's that.
-
40:36
[MUSIC]
-
40:40
No matter where we run it, we're getting the same thing.
-
40:44
[MUSIC]
-
40:56
Right, okay.
-
41:03
Yeah, this isn't doing what I want.
-
41:06
So why isn't this working?
-
41:07
[MUSIC]
-
41:14
If I do Python, I get that, okay, cool.
-
41:17
So if I do
-
41:20
.virtualenvs\game_api\Scripts\python.exe,
-
41:30
I get that, okay, as I should.
-
41:34
So if I do that and then do that,
-
41:37
game_api\Scripts\apistar-script.py, I
-
41:47
get that.
-
41:48
And if I get that and do run, I get no app.py module exists.
-
41:53
Cool, we're a step closer.
-
41:56
So now let's go in here and run that again.
-
41:58
[MUSIC]
-
42:01
Exact same thing.
-
42:04
Okay, app.py, we don't have anything weird on our line 1.
-
42:08
Everything here is exactly what we got given earlier, okay?
-
42:13
[MUSIC]
-
42:21
Okay, and if I do,
-
42:23
[MUSIC]
-
42:26
This apistar.exe, that's fine.
-
42:31
See this is what's so weird, right, because,
-
42:34
[MUSIC]
-
42:40
I will read your thing here in the second poss, that looks useful.
-
42:43
[MUSIC]
-
42:48
Okay, a magic comment must be placed into the source file either as the first or
-
42:52
second line in the file, such as # coding = encoding name.
-
42:56
Right, which we did that with this thing here, right?
-
43:01
To aid with platforms such as Windows which add Unicode BOM marks to
-
43:06
the beginning of Unicode files, the UTF-8 signature \xef\xbb\xbf will be interpreted
-
43:13
as Unicode 8 encoding as well, even if no magic encoding comment is given.
-
43:19
[MUSIC]
-
43:29
No.
-
43:30
Okay, we're just not gonna worry about this for now.
-
43:32
I will talk to Tom, we will figure out what is going on.
-
43:37
We're going to try out,
-
43:38
[MUSIC]
-
43:44
We're gonna try out hug instead.
-
43:47
[MUSIC]
-
43:53
Yeah, this is just weird.
-
43:56
I don't know why this is failing.
-
44:00
[MUSIC]
-
44:05
And if you try running API start script-exe.
-
44:08
[MUSIC]
-
44:16
We don't have a script.exe, so we just have those two.
-
44:19
[MUSIC]
-
44:24
/apistar run?
-
44:25
[MUSIC]
-
44:30
No we don't have a command for that.
-
44:31
[MUSIC]
-
44:37
No app module exists, yeah, we'll figure it out later.
-
44:42
I will talk to Tom, I will figure out what he is doing or
-
44:47
not doing there that we have missed.
-
44:49
But let's try out hug for now, we've got an hour and 15 left, let's try out hug.
-
44:56
So let's go to our settings.
-
45:00
And we don't need any of these, except for we need pip and we need setuptools.
-
45:06
So the rest of those can go away.
-
45:10
Honestly, I really don't know why this didn't work.
-
45:16
Yep, let's add hug, that's all we have to do, okay.
-
45:21
So then we can go and build, and I really, apistar was cool,
-
45:25
I really liked the thing.
-
45:36
I have a Linux virtual machine that I could try and do this on.
-
45:40
But I don't necessarily wanna try and do that at the moment,
-
45:45
because we'd have to mess with a bunch of stuff.
-
45:49
And you all would get really bored with all the stuff I was messing with.
-
45:54
So let's not do that.
-
45:58
All right, let's try this instead.
-
46:01
So we'll make a thing here, so we'll do import hug.
-
46:08
And we do @hug.get.
-
46:11
And we say what the things is.
-
46:12
And let's just call this hello and name=None.
-
46:22
And we'll return message,
-
46:28
and this will say Hello from Hug!
-
46:38
If name is None, we'll do that.
-
46:41
Otherwise we'll return {'message': 'Hello from Hug,
-
46:52
{name}, we'll make this an f string.
-
46:56
And we'll put that there, okay.
-
46:59
[MUSIC]
-
47:03
Okay, so let's see if this works.
-
47:09
And then we have to run that with hug -f app.py.
-
47:15
[MUSIC]
-
47:19
Access is denied?
-
47:20
[MUSIC]
-
47:24
Access is denied for what?
-
47:25
[MUSIC]
-
47:29
For hug script.
-
47:31
[MUSIC]
-
47:33
Let's restart the terminal here.
-
47:34
[MUSIC]
-
47:41
Can't pickle module objects when API, what?
-
47:47
[MUSIC]
-
47:50
All right, now look you all,
-
47:52
there's no reason to treat Windows as some second class thing.
-
47:56
Minimal Hug program not working on Windows 10.
-
47:59
Uh-huh, hey look, they've got something pretty similar to what I did.
-
48:04
[MUSIC]
-
48:12
hug -f app.py --manual_reload.
-
48:20
Sure, Python, you can access the Internet, that's fine.
-
48:23
[MUSIC]
-
48:26
Okay, so now it says I should be able to go here, and get that.
-
48:34
We got a thing, and if I did name,
-
48:39
nope, name=Kenneth.
-
48:43
[MUSIC]
-
48:46
All right, so there we go, we got a thing, right?
-
48:48
[MUSIC]
-
48:50
So cool, something came up.
-
48:55
[LAUGH] We have to manually reload,
-
48:58
which is not fun, but that's fine.
-
49:03
We can manually reload if we need to.
-
49:06
[MUSIC]
-
49:09
Okay, we're good, right?
-
49:16
We can get ourselves back.
-
49:19
Okay, so that's installing hug, that's our basic thing here.
-
49:33
How do we accept,
-
49:34
[MUSIC]
-
49:37
How do we accept arguments on our line there?
-
49:40
So we can run it with uwsgi things, which that's cool.
-
49:43
[MUSIC]
-
49:49
We can do type annotations, that's fun.
-
49:51
[MUSIC]
-
49:56
We can do all sorts of different things.
-
49:58
[MUSIC]
-
50:03
Okay, and so that's cool.
-
50:05
So then we can do Like output equals
-
50:09
hug.output_format.json, so
-
50:14
that way it knows that it's outputting JSON, let's open this up,
-
50:19
and network refresh.
-
50:26
All, if we look here, we get a document.
-
50:33
Our response headers are application/json, so nice.
-
50:37
So it gave us application/json as the response.
-
50:42
So cool, that works like I would expect.
-
50:47
I didn't restart, so that's kind of funny that that even worked.
-
50:58
[SOUND]
-
51:01
Let's try
-
51:07
that again.
-
51:14
Maybe it does JSON by default.
-
51:21
And then we can do input formatters so that we control how the input comes in.
-
51:24
And we can split it up and we can do that.
-
51:28
There was A sync, which is cool.
-
51:30
All right.
-
51:34
[MUSIC]
-
51:48
Okay, so I'm assuming it, just by default, sends out JSON.
-
52:05
I wonder what the examples thing is?
-
52:09
That's interesting.
-
52:16
Okay, so that's our Quick Start.
-
52:27
Architecture, And that's cool.
-
52:37
So we can bring in a command line one, let's try that too.
-
52:46
@hug.cli(), yeah, sure.
-
52:50
if _ _ name _ _ == '_ _main_ _': Not add,
-
52:58
it would be hello.interface.cli.
-
53:07
Okay.
-
53:11
So, that's cool.
-
53:12
So, now, I should be able to go here and
-
53:17
I should be able to do app.py, what does that do?
-
53:22
That just wants to open it using Visual Studio.
-
53:24
I don't need you to open it using Visual Studio, why don't I just do app?
-
53:27
No.
-
53:37
That sends back that, name=Kenneth.
-
53:44
Cool, okay, that's neat.
-
53:49
That's a cool little feature.
-
53:52
And I can still hit it from a url as well.
-
53:59
Okay.
-
54:01
So, I can do it as either an external API, or
-
54:06
I can do it as a command line interface.
-
54:09
That's kinda neat.
-
54:13
Okay, that's a cool idea.
-
54:22
Looks like it's routing again here.
-
54:27
Okay, so there's that.
-
54:29
So then I can also declare a router for like this.
-
54:40
Okay, can train routers.
-
54:50
Okay, I'm not gonna worry about that for now.
-
54:53
Transform output requires, ACDP router.
-
54:59
These docs are really hard to read.
-
55:03
It would be nice to not have such hard to read docs, hug.
-
55:16
I wanna be able to provide an argument to the URL.
-
55:31
But it doesn't look like I can do that.
-
55:41
Cuz what I'd like to be able to do is go hello/Kenneth.
-
55:46
Which I realize that's not currently running.
-
55:57
That's still gonna die, yep.
-
56:09
So yeah, I'd like to be able to go /hello/kenneth and
-
56:14
it knows what I'm trying to do.
-
56:17
So like this would be hello.
-
56:29
Come on.
-
56:36
That's really annoying.
-
56:47
Right, so let's see, it doesn't know what I'm doing there.
-
56:51
So it knows it can take a name but not,
-
57:05
Not, It doesn't know
-
57:10
how to take that extra argument, which is too bad.
-
57:19
Because that will be cool and nice to have.
-
57:24
So technical's routing again.
-
57:26
It seems like it should be in routing.
-
57:31
Okay, so we go to there and that does that, but
-
57:34
then we can also do one separate like this.
-
57:42
Hug routers, because they're so powerful, is duplication between routers.
-
57:44
For instance, if you decide you want every router to run the 404 page.
-
57:53
Okay, so we're just defining this thing here, and
-
57:57
then we're adding to the URLs like that.
-
58:00
Yeah, okay.
-
58:06
Function to call on the data returned by the function to transform it.
-
58:10
Output format to apply requires list of things
-
58:15
have to be there, can actually get, so on and so forth.
-
58:21
Things that we're able to make.
-
58:26
Ha, there we go.
-
58:27
When blank notation is found in the URL, that's not exactly what I want.
-
58:32
But I can do
-
58:40
/name?
-
58:55
And then, okay, so that worked.
-
59:03
And then if I go to just hello, that one fails because it expects that.
-
59:16
But I bet I can do that.
-
59:32
That one works.
-
59:33
That one works.
-
59:37
That one works.
-
59:39
That one works.
-
59:41
Okay, so we have a thing that works for this.
-
59:45
It's a little weird, but it works.
-
59:49
Yeah, that's just strange.
-
1:00:09
That's just strange.
-
1:00:11
[LAUGH] I don't wanna try and do the virtual ENV thing or
-
1:00:15
not virtual ENV, the virtual box.
-
1:00:18
I don't know that I could enough done on that to make it,
-
1:00:24
unless you all wanna to help me figure it out.
-
1:00:26
So it's been quite a while since I've done like shared folders and stuff like that.
-
1:00:30
So if you all are down with helping me, get it all running,
-
1:00:35
then we will do Python running on a virtual machine and
-
1:00:40
editing the code on Windows and testing it within our Windows browser.
-
1:00:43
So let me know in chat if you're down for that,
-
1:00:47
if not, we'll play a little bit more with Hug and see what we can do there.
-
1:00:53
So I'll give you a couple minutes for chat and I think I saw some questions earlier.
-
1:00:59
So I'm gonna scroll back up through text here, and see what I see,
-
1:01:06
Gal, I answered that one a while ago
-
1:01:15
[SOUND] I mentioned people talking about Go a way back, yeah.
-
1:01:24
All right, so it says we've got new messages, okay, Paul says shared folders,
-
1:01:28
let's do it, let's just test this out.
-
1:01:30
So we're gonna do API Star, we're gonna do VirtualBox stuff instead,
-
1:01:37
so let's get VirtualBox running, and no, I'm running Arch.
-
1:01:44
[LAUGH] Pause, I'm running Arch,
-
1:01:48
just because I wanted to make things more difficult,
-
1:01:55
but I'm almost positive that I have basic stuff working on this.
-
1:02:03
I've got Python installed if I remember correctly.
-
1:02:11
Duncan, that's cool, if you don't like the ads
-
1:02:16
don't watch the ads, but yeah, that's cool.
-
1:02:20
Enjoy your day, have a good day.
-
1:02:25
[SOUND] Sweet, so we're gonna run this as route two,
-
1:02:33
because why not, okay, so
-
1:02:37
let's make sure we've got Python, yeah.
-
1:02:43
So we've got Python 3, so
-
1:02:47
I want to setup the shared folders thing.
-
1:02:54
Shared folders, you have to use the devices menu, once this is up and
-
1:02:58
running then you can insert guest editions.
-
1:03:00
I think I've already have got guest additions installed.
-
1:03:09
So that lets me do that one, I want that folder, sure, that's fine.
-
1:03:19
Add_amount, sure.
-
1:03:29
I guess q tells us with Pac-man, let's say OK there.
-
1:03:37
So this is like Pac-man dash S virtualbox-guest-utils.
-
1:03:51
Sure, go for that, done, all right.
-
1:04:11
Auto-mount and make permanent.
-
1:04:12
Sure, I mean, we can do that.
-
1:04:23
I guess something I don't know is where those show up.
-
1:04:29
Any guesses on that?
-
1:04:29
Humanoid, we were playing with API Star,
-
1:04:35
and it started having all sorts of issues,
-
1:04:41
so we are not doing that anymore.
-
1:04:45
But we're gonna go back to API Star once we get the virtual machine working.
-
1:05:00
Network settings.
-
1:05:07
Port forwarding.
-
1:05:11
Let's not worry about those for now.
-
1:05:24
What is this ifconfig, no?
-
1:05:27
Ipconfig, no?
-
1:05:55
Ah-ha, there we go.
-
1:05:57
So I'm gonna loop back, of course,
-
1:06:01
and then do I only have it as,
-
1:06:10
I have IP6?
-
1:06:54
All right, well, let's see if we can get this to work first.
-
1:06:58
So where do I need to go for the shared folders.
-
1:07:03
[SOUND]
-
1:07:06
Is that in
-
1:07:12
Vbox?
-
1:07:23
All right, those aren't directories,
-
1:07:33
It's virtual box guest utils Open RC.
-
1:07:42
Was it that,a Pac-Man thing?
-
1:07:55
No, guess sources menu, once it's up and running, enter guests editions image,
-
1:08:02
install virtual box guest details which we did that.
-
1:08:10
Check out tutorial, that's cool.
-
1:08:14
Don't mount the Iso, that method works with many of the but not with Arch Linux.
-
1:08:20
Yeah, so we'll figure this out in a minute.
-
1:08:27
So humanoid, what our plan was, was to use this API
-
1:08:31
start package from Tom Christie which is pretty cool,
-
1:08:36
it's a neat package, and we're gonna use this to build an API,
-
1:08:42
but it is failing catastrophically on Windows.
-
1:08:46
So, With no real reason why,
-
1:08:53
it's dying through a Badly encoded thing.
-
1:09:01
So we were gonna try and get
-
1:09:07
it working on a Linux Virtual Machine, but pause is looking up stuff at the moment.
-
1:09:14
So we're gonna find out what What Pause finds out, and then we will try more.
-
1:09:20
And it's too bad, because I really
-
1:09:25
like API Stars way of working.
-
1:09:30
I wonder about doing API Star, so
-
1:09:34
let's do this while you're looking all of that up Pause.
-
1:09:42
And just send me a message when you find something.
-
1:09:47
I wanna try something new, don't wanna have a lot of downtime here y'all.
-
1:09:54
Get rid of those things again, All right, and let's install API Star again.
-
1:10:01
Let's go ahead and install that package.
-
1:10:09
And then let's install Gunicorn and
-
1:10:12
Mindheld as well And see what we can get with that.
-
1:10:18
Because we could totally just run the app, right?
-
1:10:26
Just like we would do in production.
-
1:10:29
But we can tell Unicorn to do reload and stuff, if I remember correctly.
-
1:10:36
So, let's try that out.
-
1:10:44
Error occurred.
-
1:10:46
What error occurred?
-
1:10:58
Meinheld does not like Windows?
-
1:11:01
[SOUND] Is that the problem?
-
1:11:09
With e-compliant web server, takes a measure, greenlit,
-
1:11:14
and Pico EV, can also serve through Unicorn.
-
1:11:23
Okay.
-
1:11:29
Okay.
-
1:11:31
Let's not worry about the worker class then.
-
1:11:36
So what we wanna do here is we wanna do API star.
-
1:11:41
I'll just run that, and it tells me what my things are, API star new dot
-
1:11:46
layout equals Layout equals minimal.
-
1:11:58
Okay, and so then we've got app.pi, which this has our app in it.
-
1:12:04
Okay, pause this thing to try.
-
1:12:07
Okay, let's try this out.
-
1:12:08
So mount -t vboxsf,
-
1:12:14
shared folder name, which I think was Projects.
-
1:12:22
/media/projects.
-
1:12:28
No such file or directory.
-
1:12:30
Let's check this one out again now.
-
1:12:37
Shared folder settings.
-
1:12:38
So, folder name is projects.
-
1:12:43
Yeah?
-
1:12:44
That all looks right.
-
1:12:53
We don't have any such thing as slash media.
-
1:12:55
Do I need to restart the thing?
-
1:13:04
Access a guess server
-
1:13:20
Maybe that's it.
-
1:13:25
No.
-
1:13:36
Nothing in there about Mount.
-
1:13:41
Nothing in there about VBoxSF.
-
1:13:42
All right, I'll let you keep looking, I'm gonna try and do this thing.
-
1:13:47
So what I wanna do here is I wanna do gunicorn app colon app..wsgi.
-
1:13:55
Workers equals 1, bind 8000,
-
1:14:01
head equals pid, whatever the pid is.
-
1:14:09
No module named FCNTL.
-
1:14:12
But you installed gunicorn, why?
-
1:14:24
That's the line
-
1:14:31
Install FCNTL.
-
1:14:40
We can't install that because it doesn't have one for Windows.
-
1:14:56
Okay, let's see.
-
1:14:59
Does it like lower case p, maybe?
-
1:15:06
No.
-
1:15:13
Slash media doesn't exist.
-
1:15:28
Arch, shared folders.
-
1:15:42
Shared Folders
-
1:15:53
The reasoning must be in that.
-
1:16:05
Vboxmanage?
-
1:16:10
No.
-
1:16:16
Let's just not worry about this for right now.
-
1:16:19
Alright, I'm not sure that this is
-
1:16:32
[SOUND]
-
1:16:43
Add users to a group, so
-
1:16:46
I wanna do g pass to bd-a root and
-
1:16:51
what was that group?
-
1:16:54
vboxsf.
-
1:16:57
All right,
-
1:17:03
mount-tvboxsf
-
1:17:11
projects slash
-
1:17:32
Okay, so now let's go look in here.
-
1:17:34
Check it out, there's our stuff, sweet.
-
1:17:37
[LAUGH] All right.
-
1:17:41
So now let's go into game_api.
-
1:17:45
All right,
-
1:17:48
python -m venv venv.
-
1:17:55
Read only file system.
-
1:17:56
Okay, mkdir /.virtualemvs.
-
1:18:00
Python-mbenv.virtualenvs/gameapi
-
1:18:09
gameenv.
-
1:18:16
Source virtual_emvs game_env bin activate.
-
1:18:19
Get install API star, it's installing things,
-
1:18:26
let's see if this does if anything at all.
-
1:18:32
Okay, so we have things installed,
-
1:18:37
cool, so now, not that.
-
1:18:41
How do we run this?
-
1:18:42
We just do apistar, right?
-
1:18:47
So, apistar run.
-
1:18:52
Okay.
-
1:18:53
The problem is though, now that's bound to local host 80/80 which we can't get to.
-
1:18:59
Right. So I go to 80/80 I get nothing.
-
1:19:07
So I need to rebind that to something else.
-
1:19:13
So how can I rebind that?
-
1:19:29
Settings, those aren't the settings I'm looking for.
-
1:19:35
Those aren't the settings I'm looking for.
-
1:19:45
Store configuration variables in there?
-
1:19:46
Yeah, I should.
-
1:19:48
But I don't want to at the moment.
-
1:20:08
Settings like that, static files, building websites, generating schemas,
-
1:20:13
those are all great But I need,
-
1:20:20
To have it listen on all ports.
-
1:20:28
Okay, so let's start looking at apistar/settings.
-
1:20:34
[MUSIC]
-
1:20:38
That didn't help, okay, [LAUGH] environment?
-
1:20:44
No. [MUSIC]
-
1:20:48
App.py
-
1:20:49
[MUSIC]
-
1:20:58
Path exception,
-
1:21:00
[MUSIC]
-
1:21:04
We're gonna get this running, you all.
-
1:21:06
Just a second and we're gonna have it.
-
1:21:08
[MUSIC]
-
1:21:11
Okay, let's try this,
-
1:21:15
let's try HOST=0.0.0.0.
-
1:21:20
apistar run.
-
1:21:21
[MUSIC]
-
1:21:24
Still running on that, okay.
-
1:21:25
HOST equals.
-
1:21:32
Still running on that.
-
1:21:33
[MUSIC]
-
1:21:41
There we go.
-
1:21:42
All right, so now if we try to go to 10 dot.
-
1:21:50
Man, I forgot what it was now.
-
1:21:52
IP address, okay, 10.0.2.15.
-
1:22:04
:8080, did we get anything?
-
1:22:08
[MUSIC]
-
1:22:16
No, okay.
-
1:22:18
So what was that other address that we had?
-
1:22:22
We had 10.0.2.255.
-
1:22:26
[MUSIC]
-
1:22:32
And that one's not working, okay.
-
1:22:38
Network.
-
1:22:40
Attach to NAT.
-
1:22:42
[MUSIC]
-
1:22:53
Host IP 8080.
-
1:22:56
Or no, not host IP, host port to guest port.
-
1:23:00
[MUSIC]
-
1:23:07
Okay, let's try running this like normal.
-
1:23:12
And let's try going to 8080.
-
1:23:19
Nothing.
-
1:23:25
Yeah, that one shouldn't work.
-
1:23:38
We got it, all right.
-
1:23:40
[LAUGH] Okay, so we have a thing that sort of kind of working.
-
1:23:49
So,
-
1:23:51
[MUSIC]
-
1:23:52
Let's see if we can fix this, let's see if we can change this.
-
1:23:55
Let's add a new route that does add and we'll say num1
-
1:24:01
equals by default 0, and num2 equals by default 0.
-
1:24:07
And we're going to return sum, and this will be num1 + num2.
-
1:24:17
Okay, and we'll say here, this should be of type complex.
-
1:24:20
[MUSIC]
-
1:24:24
And this here should be of type complex.
-
1:24:27
Not that it matters, but we're gonna just do that.
-
1:24:30
Okay, and so then here we'll do add, and this goes to add.
-
1:24:35
And if I look over here, it should have restarted.
-
1:24:39
But it doesn't let me do it on the complex thing, so that's fine.
-
1:24:42
[MUSIC]
-
1:24:44
Okay, run that again.
-
1:24:47
Refresh that, that gives us that, and
-
1:24:51
if we go to /add, message not found.
-
1:24:55
Is it because of that trailing slash?
-
1:24:59
Okay, so yeah, we can't add num, we can't add.
-
1:25:03
[MUSIC]
-
1:25:13
But what?
-
1:25:16
[MUSIC]
-
1:25:23
Okay, so now we'll try num1=5 and num2=9.
-
1:25:30
[MUSIC]
-
1:25:42
try num1 = complex(num1),
-
1:25:48
num2 = complex(num2),
-
1:25:54
except ValueError.
-
1:25:57
[MUSIC]
-
1:26:01
return {'message': "Those aren't
-
1:26:07
numbers!"} else: { 'message'
-
1:26:12
[MUSIC]
-
1:26:15
str(num1+ num2) }
-
1:26:20
[MUSIC]
-
1:26:22
Not exactly right, but close.
-
1:26:25
[LAUGH]
-
1:26:27
[MUSIC]
-
1:26:30
Okay,
-
1:26:31
[MUSIC]
-
1:26:33
So we have our first step, we have a thing.
-
1:26:36
Also it's kinda cool that I can code locally and
-
1:26:41
pull it up on a virtual box, that's kinda neat.
-
1:26:46
So thank you all for your help on getting that to work because that's cool.
-
1:26:50
I'm very happy to have that working.
-
1:26:52
[MUSIC]
-
1:26:55
Okay, let's look down here at the routing again.
-
1:26:57
So what we could do here is we could do,
-
1:27:02
[MUSIC]
-
1:27:06
Different routes here.
-
1:27:08
So what can we do with this?
-
1:27:10
So we can do routes, we can get a route like that.
-
1:27:14
[MUSIC]
-
1:27:17
We can use include to include a set of routes.
-
1:27:21
So, excuse me.
-
1:27:22
So we could do something like this where it was like add routes.
-
1:27:27
It's gonna be a list of routes so we can do route of,
-
1:27:31
[MUSIC]
-
1:27:34
That as GET, and that'll just do add.
-
1:27:39
[MUSIC]
-
1:27:45
Let's actually go back to these being 0.
-
1:27:47
[MUSIC]
-
1:27:52
And let's look up,
-
1:27:53
[MUSIC]
-
1:27:58
Complex.
-
1:28:00
[MUSIC]
-
1:28:02
So we pass in the real, and we pass in the imaginary, and
-
1:28:06
really I wanna try it being an int or a float.
-
1:28:10
I just wanna convert it to a number.
-
1:28:12
[MUSIC]
-
1:28:18
Okay, so let's just try this.
-
1:28:19
So if num1.isnumeric,
-
1:28:25
[MUSIC]
-
1:28:32
That's it, so all of them are numbers.
-
1:28:34
Okay, if num1.isnumeric, if '.'
-
1:28:41
in num1, num1 = float [num1]
-
1:28:47
else: num1 = int[num1]
-
1:28:52
[MUSIC]
-
1:28:54
else: num1 = 0
-
1:28:57
[MUSIC]
-
1:29:02
We're not gonna do this, def convert_num(num).
-
1:29:09
[MUSIC]
-
1:29:25
return float(num).
-
1:29:26
return int num.
-
1:29:28
[MUSIC]
-
1:29:29
return int(num),
-
1:29:31
[MUSIC]
-
1:29:34
return 0.
-
1:29:35
[MUSIC]
-
1:29:44
num1 = convert_num(num1).
-
1:29:49
[MUSIC]
-
1:29:52
Delete all of that, delete all of that, delete that.
-
1:29:59
Bring that back, that's gonna be the str(num1 + num2), all right.
-
1:30:01
[MUSIC]
-
1:30:06
Okay.
-
1:30:07
[MUSIC]
-
1:30:10
Darshan, my version of PyCharm is the paid one.
-
1:30:13
But the free one should work fine.
-
1:30:17
You shouldn't have to worry about anything weird there.
-
1:30:23
[MUSIC]
-
1:30:28
So we wanna do this where we can have num1/num2.
-
1:30:35
That would also go to GET.
-
1:30:38
Uh-huh.
-
1:30:40
So then we take that out and
-
1:30:44
we would do Include at add_routes.
-
1:30:50
And then a comma in the end.
-
1:30:52
[MUSIC]
-
1:30:58
Okay, my syntax isn't invalid.
-
1:31:00
It's running.
-
1:31:01
If we come over here and we refresh this.
-
1:31:05
Go to /add, we get 0.
-
1:31:06
If we go to /add/5/9, not found.
-
1:31:14
14, yay.
-
1:31:16
We got back the number we expected.
-
1:31:21
Okay, I don't know about you all, but that was way more work than it needed to be.
-
1:31:26
[LAUGH] But that's cool.
-
1:31:28
Okay, so I also wanted to check out docs.
-
1:31:36
So we have it working to where we can
-
1:31:41
hit our API on the virtual machine, which is cool.
-
1:31:46
Thanks again poss for helping me get that working.
-
1:31:50
We can do a URL that goes to somewhere.
-
1:31:58
And it will work, right?
-
1:32:02
So we can do /add and we get 0, because nothing came in.
-
1:32:06
We can do /add/?num1 = 10 and we should get back 10?
-
1:32:12
No?
-
1:32:14
Am I not allowed to do it that way?
-
1:32:21
Num2 = 15.
-
1:32:29
So I lost that ability.
-
1:32:37
Weird.
-
1:32:40
Why did I lose that one?
-
1:32:47
[SOUND] None.
-
1:32:53
None.
-
1:32:53
None, none, right.
-
1:32:58
Do I need to take off this slash?
-
1:33:02
No.
-
1:33:05
What if I take off this and I put in add right there?
-
1:33:11
[SOUND]
-
1:33:14
Not found.
-
1:33:19
[SOUND] I get 0.
-
1:33:22
[SOUND] Okay.
-
1:33:24
Whatever.
-
1:33:25
I don't care.
-
1:33:26
I'm gonna leave it like it was cuz it worked.
-
1:33:29
We're gonna leave that alone.
-
1:33:31
Okay, so we did that.
-
1:33:34
We get add, we get 0, right, okay.
-
1:33:37
So, let's look over here at these docs again, which this is so
-
1:33:42
cool that it lists docs just right here because how nice is that?
-
1:33:51
How can I make those, and check this out.
-
1:33:54
I can just do those and it gives me back, that's so cool.
-
1:34:01
[LAUGH] I love Tom Christie and the stuff he comes up with because
-
1:34:06
he comes up with some just absolutely amazing things.
-
1:34:12
If you all don't know who Tom Christie is, you should go and look him up.
-
1:34:17
He comes up with amazing, amazing bits of work.
-
1:34:22
Okay, so let's see what else we can do here.
-
1:34:27
So, Well so
-
1:34:32
poss, they're nice docs but they're also docs based on my endpoints.
-
1:34:38
That's what makes it so cool is it's not things I had to write.
-
1:34:44
It's stuff that Tom introspected and wrote.
-
1:34:50
So, let's do a thing here where we do Add two numbers
-
1:34:55
together and get the sum.
-
1:35:01
And together has an e in the middle of it.
-
1:35:04
If we refresh that right there, does that change anything?
-
1:35:09
[MUSIC]
-
1:35:13
No.
-
1:35:14
Darn, that would've been awesome.
-
1:35:17
Okay, so let's look now at this building websites part.
-
1:35:29
Sorry, just got a thousand pings on my phone all of a sudden.
-
1:35:35
Okay, cool.
-
1:35:35
I don't need to see any of those.
-
1:35:37
Thanks phone for interrupting me.
-
1:35:39
Okay, so now we can do templates.
-
1:35:43
So let's add a templating thing here.
-
1:35:48
So templates and then let's add a new file.
-
1:35:56
And we put in doctype.
-
1:36:01
Which I can't type apparently.
-
1:36:04
No? Not gonna do.
-
1:36:06
[SOUND] There we go.
-
1:36:12
Game API.
-
1:36:14
[SOUND] h1, high name.
-
1:36:25
Okay, so then we can take our welcome endpoint here.
-
1:36:33
Shrink this one a little bit.
-
1:36:35
So we get a name.
-
1:36:38
And we need to import templates, that comes from here.
-
1:36:44
App, Route, Templates, uh-huh.
-
1:36:46
[MUSIC]
-
1:36:50
And then he has me importing OS, but I don't see any usage of OS there so
-
1:36:54
we're not gonna worry about OS for now.
-
1:36:57
Okay, so then down here in this one,
-
1:37:02
templates, Templates.
-
1:37:06
[SOUND] name has to be a str, has to be a string.
-
1:37:17
And we're gonna do index = templates.get_template.
-
1:37:23
We wanna get index.html, or return index.render, name=name.
-
1:37:30
[SOUND] And we already have that route there for welcome.
-
1:37:37
And then we need to add our settings, so let's do settings, is a dictionary.
-
1:37:42
TEMPLATES is also a dictionary.
-
1:37:48
DIRS is a list with templates in it,
-
1:37:53
and then we have to specify our settings inside there.
-
1:37:57
[SOUND] Okay, let's make sure this is all running.
-
1:38:06
No?
-
1:38:06
[MUSIC]
-
1:38:15
Unresolved reference to templates.
-
1:38:17
[MUSIC]
-
1:38:22
What?
-
1:38:22
[MUSIC]
-
1:38:29
I'm gonna guess it's Template.
-
1:38:30
[MUSIC]
-
1:38:39
What's up, Tom?
-
1:38:41
What's up, man?
-
1:38:43
Why do I keep finding things here that aren't working?
-
1:38:45
[MUSIC]
-
1:39:03
Can you define it all in here?
-
1:39:06
Templates.
-
1:39:07
[MUSIC]
-
1:39:10
All right, let's do a thing that I normally hate doing.
-
1:39:14
from apistar import star.
-
1:39:16
[SOUND] I hate doing that.
-
1:39:20
[MUSIC]
-
1:39:27
Okay, let's try running that.
-
1:39:32
Templates is not defined.
-
1:39:38
Okay.
-
1:39:42
So let's go into Python.
-
1:39:45
from apistar import star.
-
1:39:56
Yeah, that didn't work.
-
1:39:59
from apistar.templating import Templates.
-
1:40:05
Okay, so that one works.
-
1:40:07
So let's just import that manually.
-
1:40:12
from apistar.templating import Templates.
-
1:40:20
And t comes after s, so we'll put that there.
-
1:40:25
And then put that there.
-
1:40:29
Okay.
-
1:40:31
And then let's run that again.
-
1:40:34
Okay.
-
1:40:36
And then if I go to here, I get hi name!
-
1:40:42
And if I say name equals Kenneth, I get nothing.
-
1:40:52
If I go to that, I get message not found.
-
1:41:19
But where did you get the username from?
-
1:41:23
Did I do my template wrong?
-
1:41:24
I did.
-
1:41:32
Okay.
-
1:41:36
Close that.
-
1:41:40
There's the name.
-
1:41:41
All right, cool.
-
1:41:42
[LAUGH] So we have something.
-
1:41:46
Something is coming out, okay.
-
1:41:49
So, cool.
-
1:41:51
We've gotta thing.
-
1:41:53
We're slowly working our way through this.
-
1:41:55
Now I wanna see what this thing is here.
-
1:41:56
So you can also use the template component to inject a single template instance
-
1:42:01
as a view argument.
-
1:42:03
Def hello user name string index template.
-
1:42:07
So, that would be here.
-
1:42:12
And then we do index.render,
-
1:42:16
that will attempt to locate index.html based on the argument name of index.
-
1:42:22
I see.
-
1:42:24
That's kinda cool.
-
1:42:25
So let's try that.
-
1:42:28
So here we can do index is a template.
-
1:42:33
And we just return index.render.
-
1:42:34
So we should still get the same thing.
-
1:42:39
We do.
-
1:42:41
Let's change this just to make sure that it's working.
-
1:42:45
Welcome to the API.
-
1:42:50
Boom! Okay.
-
1:42:52
That's pretty cool.
-
1:42:54
Good job there, Tom.
-
1:42:56
We had an issue but we gotta thing.
-
1:43:00
It's okay.
-
1:43:01
All right, so now let's try one more thing here just to make sure it's working.
-
1:43:07
So we'll make a directory called Static and
-
1:43:13
then inside Static, we'll make a new directory called, CSS.
-
1:43:18
And inside there we'll make a new file, which we'll call games.css.
-
1:43:27
And we'll just do body, background color.
-
1:43:35
Seagreen, and we'll do color as bisque
-
1:43:41
just to do absolutely horrible things
-
1:43:47
that nobody should ever really see.
-
1:43:53
Okay, and then here we say statics, and we say dir,
-
1:43:58
and then we say static, cuz I don't know why that
-
1:44:03
is duplicated there, or plural rather.
-
1:44:08
Okay, then from statics we also want to call serve static.
-
1:44:14
I'm wondering if that's actually needed cuz I have a feeling that the static
-
1:44:21
routes here might be working.
-
1:44:28
Let's test that real quick.
-
1:44:33
What would we do up here?
-
1:44:35
We'd do link href
-
1:44:40
equals static.
-
1:44:49
Css games.css.
-
1:44:51
url equals stylesheet.
-
1:45:02
And that works. I don't have add anything.
-
1:45:05
So that means I can come over here and take out that part.
-
1:45:12
So that still works.
-
1:45:14
All right, that looks horrible, but that's fine.
-
1:45:18
It's okay if it looks horrible.
-
1:45:20
We can make it look nicer later if we really want to, okay.
-
1:45:27
[SOUND] I kind of feel bad we're at 13 minutes left.
-
1:45:34
It's kind of weird to start something at this point.
-
1:45:35
But, any questions?
-
1:45:38
Anything weird for everybody?
-
1:45:44
Anything that you all are confused about or
-
1:45:47
have questions about with all of this, let me know.
-
1:45:54
Tell me what you're thinking?
-
1:45:57
No pause, no, no pause,
-
1:46:01
those are just me getting real lucky, just really really lucky.
-
1:46:08
So I'm just kinda playing around with this stuff.
-
1:46:13
So my plan, there's a website called
-
1:46:18
Board Game Geek, which is kind of a horrible website.
-
1:46:25
Like it's cool as far as he can go pop open a game and
-
1:46:30
find out a bunch of data about it.
-
1:46:31
That's cool.
-
1:46:34
It's full of comments I don't care about,
-
1:46:36
rankings I don't care about, stuff like that.
-
1:46:39
My thought was that we'll build a little API off of API Sta.
-
1:46:45
That we can add a new game to the records and we can
-
1:46:52
search for games, we can pull a game out.
-
1:46:57
So, that's my plan.
-
1:46:59
I don't believe that plan is going to necessarily work.
-
1:47:03
And that plan is definitely not going to work today.
-
1:47:06
We're not gonna do all that today.
-
1:47:08
But then I was also thinking about trying out vue.js with that.
-
1:47:12
Cause vue.js seems kinda cool.
-
1:47:15
I haven't gotten to play with vue.js, but it seems kinda neat.
-
1:47:19
So, that'll come later.
-
1:47:24
First we'll get the basic thing working.
-
1:47:26
I was gonna do this all with Flask.
-
1:47:28
And then it was like well why do it with flask,
-
1:47:30
I'm just gonna do it with API star, cuz API star seems cool.
-
1:47:34
And API star is cool when it works.
-
1:47:39
And so far it's worked fairly well right?
-
1:47:42
So I could use Kenneth Wright's tab lib as a way of doing the data sets
-
1:47:53
I don' know if I want to do that.
-
1:48:00
I mean it's really cool, but
-
1:48:12
I mean, maybe.
-
1:48:18
Let's try it, let's see what we can do here.
-
1:48:30
Is there a way to just throw stuff in there?
-
1:48:41
See the filter is really cool.
-
1:48:44
I really like the filter.
-
1:48:48
That's basically what I want.
-
1:48:51
That's basically it.
-
1:48:52
Okay.
-
1:48:53
So, let's make a new file, and
-
1:48:57
let's call this game schema.
-
1:49:06
It's not exactly right for what I wanna do, but we'll call it that.
-
1:49:10
Well, we've got about ten minutes left.
-
1:49:12
If y'all have any questions, now's the time to ask them.
-
1:49:16
Toss your questions into chat and I will answer them before we wrap up for the day.
-
1:49:24
And next week, we'll do some of this stuff for real.
-
1:49:28
Tom was saying that there's some issues with databases and schemas.
-
1:49:32
So, I'm gonna try to stay away from that but we'll do what we can here.
-
1:49:41
So, let's import tablib.
-
1:49:44
And let's make a new thing here which is games
-
1:49:50
is tablib.Dataset, games.headers is,
-
1:49:56
and I wanna have, Name, Designer,
-
1:50:00
Player count Yep, player count.
-
1:50:11
Scores.
-
1:50:12
[MUSIC]
-
1:50:19
Let's not worry about scores for now.
-
1:50:21
Scores seems like too much work.
-
1:50:24
Okay, so then we
-
1:50:29
wanna do def read, and
-
1:50:36
with open('games.json')
-
1:50:46
as f:,
-
1:50:48
[MUSIC]
-
1:50:53
f.read,
-
1:50:54
[MUSIC]
-
1:50:58
data = f.read,
-
1:50:59
[MUSIC]
-
1:51:02
return data, there's far better ways to doing that, but whatever.
-
1:51:07
[MUSIC]
-
1:51:12
And then that would be where we'd do that.
-
1:51:14
So let's say, def write, with open('games.json',
-
1:51:21
[MUSIC]
-
1:51:25
write,
-
1:51:25
[MUSIC]
-
1:51:29
I always forget what my arguments are for this.
-
1:51:33
That's all I want, I just want w, cuz I don't want binary.
-
1:51:37
I don't wanna append, I just wanna write the whole thing, okay.
-
1:51:40
So 'w') as f:, f.write(games.json)
-
1:51:46
[MUSIC]
-
1:51:50
Okay.
-
1:51:51
[MUSIC]
-
1:51:52
So,
-
1:51:54
[MUSIC]
-
1:51:57
I should,
-
1:51:58
[MUSIC]
-
1:52:02
Be able to,
-
1:52:04
[MUSIC]
-
1:52:07
add_game, and we're gonna take name, designer,
-
1:52:13
[MUSIC]
-
1:52:19
player_count, let's actually take one more, which is duration.
-
1:52:23
[MUSIC]
-
1:52:31
Okay, so now take that off, take that off, take that off.
-
1:52:38
And I want to add the new thing.
-
1:52:44
Is there something in here about schemas, yeah.
-
1:52:47
[MUSIC]
-
1:52:52
Okay, so then we can make a schema here.
-
1:52:56
So let's go ahead and do that here, too.
-
1:53:00
from apistar import schema,
-
1:53:09
class GameSchema(schema.Object):,
-
1:53:19
properties = {'name', schema.String,
-
1:53:25
[MUSIC]
-
1:53:28
'designer', schema.String,
-
1:53:32
[MUSIC]
-
1:53:35
'player_count',
-
1:53:37
[MUSIC]
-
1:53:38
schema.
-
1:53:39
[MUSIC]
-
1:53:42
Integer,
-
1:53:43
[MUSIC]
-
1:53:46
And 'duration', schema.Integer.
-
1:53:51
[MUSIC]
-
1:53:57
And then here,
-
1:53:59
[MUSIC]
-
1:54:04
from game_schema import GameSchema.
-
1:54:09
[MUSIC]
-
1:54:11
So here we would do game: GameSchema.
-
1:54:15
[MUSIC]
-
1:54:22
And let's actually import games here, as well.
-
1:54:26
[MUSIC]
-
1:54:33
And,
-
1:54:35
[MUSIC]
-
1:54:39
Let's do,
-
1:54:41
[MUSIC]
-
1:54:44
For now, let's just do this, so message,
-
1:54:47
[MUSIC]
-
1:54:50
game, cuz I don't know what this will actually do.
-
1:54:54
[MUSIC]
-
1:54:58
So we don't care about any of those, we don't care about that.
-
1:55:02
We wanna do add, and this'll be a POST, and this would go to add_game.
-
1:55:07
[MUSIC]
-
1:55:09
And then let's open up,
-
1:55:13
[MUSIC]
-
1:55:17
Postman,
-
1:55:18
[MUSIC]
-
1:55:22
And we don't wanna go to that, we wanna go to,
-
1:55:28
[MUSIC]
-
1:55:31
localhost:8080, let's see if that works, could not get any response.
-
1:55:38
Turn off that,
-
1:55:40
[MUSIC]
-
1:55:42
Delete that,
-
1:55:43
[MUSIC]
-
1:55:53
No module named 'tablib', but
-
1:55:57
I installed tablib, didn't I?
-
1:56:01
There's tablib.
-
1:56:02
[MUSIC]
-
1:56:23
Did I not install tablib?
-
1:56:26
[MUSIC]
-
1:56:32
I didn't install tablib here, [SOUND] that's the thing I gotta do.
-
1:56:38
I don't care about installing it there, I need to install it here.
-
1:56:42
Which means that I could probably do this.
-
1:56:46
I could probably change this, nope.
-
1:56:54
Add Local, and if I went into here,
-
1:57:00
and here, and here, and here,
-
1:57:05
[MUSIC]
-
1:57:16
Would that work?
-
1:57:17
[MUSIC]
-
1:57:22
Nope, darn, okay, now let's try it.
-
1:57:26
[MUSIC]
-
1:57:42
I need more data about your thing here.
-
1:57:47
[MUSIC]
-
1:58:08
Sweet, so there's that thing, okay.
-
1:58:12
So now if I try to go to add, Method not allowed.
-
1:58:17
Let's go to POST, let's look at our Body.
-
1:58:21
And we're gonna say that the name of the game is Pictomania.
-
1:58:28
And the designer of
-
1:58:32
the game is Vlada Chavdl.
-
1:58:37
And the player-count is 6,
-
1:58:44
and the duration is 45,
-
1:58:49
and we Send that.
-
1:58:53
Player_count, Must be a valid number, Duration, Must be valid number.
-
1:58:56
Are those not numbers?
-
1:58:57
[MUSIC]
-
1:59:07
Form data.
-
1:59:09
[MUSIC]
-
1:59:16
Raw, JSON, name,
-
1:59:20
[MUSIC]
-
1:59:26
'Pictomania', 'designer',
-
1:59:32
'Vlada Chavdl',
-
1:59:35
[MUSIC]
-
1:59:38
'player_count', '5',
-
1:59:43
'duration', '45'.
-
1:59:47
[MUSIC]
-
1:59:49
Sweet, and we got one back, okay.
-
1:59:51
Now that didn't save it to the database.
-
1:59:52
We didn't do anything like that, but we definitely got one back.
-
1:59:56
So it generated one from the schema and gave us one back, that's really cool.
-
2:00:00
Okay, we're at noon, I'm stopping here.
-
2:00:04
We will do more later, but I'm gonna answer your questions.
-
2:00:08
Darshan asks, what book is best to learn Django from scratch?
-
2:00:13
I don't think you actually need a book to learn Django from scratch.
-
2:00:17
Assuming you know Python, which if you don't know Python,
-
2:00:20
don't try to learn Django, go learn Python.
-
2:00:23
But the Django tutorial and the Django Girls tutorial,
-
2:00:28
djangogirls.org and docs.djangoproject.com.
-
2:00:32
I may have those URLs wrong, but I think those are right.
-
2:00:35
Follow those tutorials, and you'll have a pretty good grasp of Django.
-
2:00:39
At that point, go start building something.
-
2:00:41
Just build whatever thing you wanna build.
-
2:00:43
Build five different things that you wanna build.
-
2:00:46
And then start looking for books,
-
2:00:49
blog posts, tutorials that fill in the questions that you have.
-
2:00:54
If you're a Treehouse student, I think the Treehouse content is pretty good for
-
2:00:56
Django.
-
2:00:57
But I'm biased, very biased.
-
2:01:00
And then once you're a little further on, Two Scoops of Django is an amazing book.
-
2:01:06
Especially once you've built two or three sites, and
-
2:01:09
you're feeling pretty comfortable.
-
2:01:10
And as pos brings up, Hello Web App is also a good book, and,
-
2:01:15
[MUSIC]
-
2:01:20
Python Crash Course has some really good Django stuff in it, and just Python stuff.
-
2:01:26
If you just wanna learn Python and stuff like that, this is also an amazing book.
-
2:01:32
So try that one out, and Ammar Yassir asks what we're building today.
-
2:01:40
Go back and watch the video, we had a whole bunch of trouble.
-
2:01:43
[LAUGH] So we're using a project called apistar to build an API.
-
2:01:49
And it just bombed catastrophically on my Windows.
-
2:01:54
Which I'm gonna have to talk to the creator of it to find out what's
-
2:01:57
going on there.
-
2:01:58
But we got it running in a virtual machine, thanks to pos's help.
-
2:02:04
And so it's working now, so it's cool.
-
2:02:08
So what we're gonna build, we'll start building more of this next week,
-
2:02:11
is we're building just a simple little API that we can send board games into.
-
2:02:16
And search for board games, and pull them back out.
-
2:02:19
So that's all we're gonna do there, but that's my plan at least.
-
2:02:26
So we'll find out, we'll find out if that all works.
-
2:02:29
But it is 12:04 PM, it's after the noon,
-
2:02:34
[MUSIC]
-
2:02:35
Bell thing, so I'm gonna call it quits for today.
-
2:02:40
But thank you, all, for watching, glad to be back.
-
2:02:45
I missed last week, I was at PyCon, which was amazing and awesome.
-
2:02:50
If you are in any sort of position to travel to PyCon next year,
-
2:02:54
it's gonna be in Cleveland, Ohio.
-
2:02:57
It's gonna be amazing, I'll be there.
-
2:02:59
I'd love to see all of you there, but yeah, check out PyCon.
-
2:03:05
All the videos are online if you wanna watch them.
-
2:03:07
Including a three-hour tutorial from me if you want to learn about Django admin.
-
2:03:12
So go to YouTube, search for PyCon 2017, go watch videos.
-
2:03:17
There's hundreds of them, and they're amazing.
-
2:03:20
So thank you very much for watching, and I'll see you all next week.
-
2:03:27
Have a great weekend.
You need to sign up for Treehouse in order to download course files.
Sign up