RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Customer Renderers
Lecture: Introduction to renderers

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Json is the most popular exchange format 00:05 for these types of services that we're building, 00:08 but there are many other types of data that we might care about, 00:10 so maybe we want to send back pictures, images 00:13 in the form of maybe say png or a jpeg; 00:16 what if we want to send back csv data to be parsed like that? 00:20 How about maybe even xml, right, 00:22 xml certainly had its heyday in the early 2000s, 00:25 but it's still a format that makes a lot of sense in some circumstances. 00:28 Think rss feeds for example; but sadly, none of these are built into pyramid 00:32 so we can't say csv for the renderer, or png or xml, 00:38 I mean, certainly in terms of png like what would that even mean, 00:41 how would it convert something to an image 00:44 if it doesn't have intimate knowledge of the data format. 00:47 So while none of these are built in, 00:49 we're going to see that we can add our own, 00:51 so let me walk you through the major steps of how we might add 00:54 csv rendering as an option to our web app, and then we'll go do that. 00:58 So the way all of these renderers work is 01:02 we create a thing that can create renderers when called upon, 01:05 so we're going to create a renderer factory whose job is to create csv renderers 01:10 something that takes an object or a list or something like this 01:13 and converts it into whatever format it's supposed to convert to. 01:16 So the api we're working against is we're going to start out 01:19 we're going to have a call function, right 01:21 so basically we create this renderer factory, 01:24 it creates a thing that when called returns something 01:26 that can execute and do the processing. 01:29 So in this case, you can see we're basically defining an internal function 01:32 that will be provided every time his runs, 01:35 and so we can create an instance of this class, configure it, 01:38 do all sorts of stuff, and then it will pass off this method. 01:41 What is render, well we're going define that here and it takes two parameters, 01:45 it takes most importantly the value, this is the thing that was returned 01:49 from that web method and we're trying to convert it 01:52 into the format that we are aiming for, in this case the csv . 01:55 Then we have the system which provides access to things like the request 01:58 and other stuff about the state of the web application. 02:01 So inside this renderer, we're going to maybe get a hold of the request 02:05 and set the content type to what type we're passing back, 02:09 in this case we're going to convert to csv, 02:11 text/ csv is the most appropriate content type, 02:14 so the browser can make the most of what you've given it, right. 02:17 Then, we're going to do some magical work that takes an arbitrary object 02:22 and converts it into a set of csv, a string that is a csv string. 02:27 We'll talk about how to do that in the demo, but let's not worry about that now 02:30 we're just going to convert value to what we're supposed to return 02:33 which is a just in memory csv string, now we just return that 02:37 and the framework handles everything else. 02:39 So we are going to create a couple of these renderers 02:42 and you'll see that we'll run into a few more nuance details 02:45 about what if we pass types to this renderer function 02:48 that it doesn't really know how to deal with, right, 02:50 we saw that with our json renderer for example. 02:53 So we'll have a couple of solutions for that, 02:55 and we'll spend the next few videos building this out. 02:58 With this framework, we can add almost any type of renderer 03:00 to our web apis that we want. 03:02 Just the existence of this class, of course doesn't do much for us 03:06 even with our configured adjusted json renderer 03:10 we saw that we had to go and actually add that renderer or replace that renderer, 03:14 so if we want to use the csv renderer, we go to somewhere in our dunder init main 03:19 and we go and say I want to go to the config, 03:22 add a renderer and then you say the name csv in a string, 03:25 that's the string that you put on the view config, 03:28 and then you pass an instance of the renderer factory, 03:30 every time it has to serialize something back 03:33 it is going to call that which will generate basically a reference 03:37 to that render function for it.


Talk Python's Mastodon Michael Kennedy's Mastodon