How to Return Files in FastAPI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i want to show you how to return files from fast api endpoints so returning files is pretty easy to do so in addition to just showing you how to return the file i'll talk about some things you may want to consider when you are returning files so to start i have a fast api app running it has one endpoint it returns hello world we can see that here and next i want to create another endpoint that will return a file so i'll call this slash cat because i want to return a picture of a cat so before i can do that i need to install another library and this is called aio files the reason being is for fast api normally you need to use asynchronous libraries to do things so fast api is an asynchronous library itself so when handling files i need to do it asynchronously so pip install a io files and i already have it but just make sure you install that before you try to do this and then once you have that installed you can import the type of response that you need so from fast api dot responses i want to import the file response and that will allow me to return a file so if i do return file response and then i put the path of the file so file slash cat.jpg and then i run my server i should be able to see the picture of the cat so let's see so slash cats and there i have a picture of a cat so that's pretty simple and like i said there are some other things you may want to consider so the first is what happens if the file doesn't exist so what if i change this to dog.jpg so i don't have a dog.jpg i only have cat so my server restarted i'll try refreshing this and i get an error and of course that's not unexpected to get an error but the thing that is unexpected is how you can handle this so here i can't just have an exception block to catch this because the exception isn't being generated in this function it's actually being generated by the thing that fast api sits on top of starlit so i have no way of just putting a try except block here to make sure that the file exists so i have to do that myself so what i'll do is i'll import from os so import os and i'll put a path to my project so i'll just call this path and we'll say um i think it's home anthony and then this is fast api file example okay and then inside of the function i'll have another one called file path another variable and this one will be os.path.join and i want to join the path that i have as a global variable and then the name of the file or the location so it'll be files and then cad.jpg and you know i'll leave it as dog so once i have that file path then i can use os path again to see if it exists so i can say if os dot path dot exists and pass in the file path here i can return the file so i'll just put file path here and if it doesn't work then i'll return like an error message so i can say file not found so now it shouldn't error out if the file doesn't exist so let's try that now so i'll go back to cat and i see file not found here as a dictionary or a json response i should say if i go back and change this to cats then this will succeed and it will go in here and return the file response so if i go back i see the cat again another thing to consider is the file type that you're returning so in this particular case i'm returning an image a jpeg image but you can return any kind of file that you want and normally what happens is it looks at the extension so dot jpg and it kind of figures out what it is so jpegs are obviously jpeg images but what if instead i had like cat dots you know abc so this is my own format or just can be any other format so like pdf i should use a real one maybe pdf or csv or so many other files i want to tell the browser what kind of file is coming so to do that i can have the media type so media type equals and then you put the type of file that it is and to get this you have to be familiar with mime types so i have the firefox documentation here or the mdn documentation and here it talks about mime types which are basically just ways of describing the type of data that is being considered so for example you can have video mp4 for mp4 videos you can have text plain for plain text text html for html you have fonts you can have images and so on so if i change the media type let's say to a font right so we know i'm dealing with an image but what if i just change the media type to a font let's see what happens so my server restarted i'll go back to cats i'll refresh and now what happens is i get this dialog to open or save the file and it says which is font wolf so it thinks that this is a font now it doesn't think it's an image any longer because if it thought it was an image it would just display the image but because it thinks it's a font it knows you can't just display a font file in the browser so you either have to download it or open it with some other program so the funny thing about browsers is they sometimes do support mistakes in the mime type so for example if i change this to image slash png remember i have a jpeg image it will still work so my server just restarted i refresh the page and it works and the reason being is because browsers can handle mistakes like that so if you specify the wrong type of image it will still try to display it as an image so it will look at it as a png first realize that it doesn't work so we'll try a jpeg because that's also a common type of image so if you want to add the media type there to give the browser more information you can you can just go to the documentation for mime types to see the different options that you have so for example if you have pdf files you can do application slash pdf but normally fast api can kind of infer what the type of file is from the the extension but you can also get away with removing the extension and then setting the mime type and then everything will work correctly so if you don't have an extension or you have a weird extension then use the media type if you have a common extension you probably don't need to specify the media type and the next thing i want to talk about are attachments so to have an attachment you need to specify a file name so if i say like mycat.jpg then what happens is when i go to the page and refresh it instead of seeing the cat in the browser i still see it because this is just the old page but let's just say i went to a home first and then i do slash cat we see it brings me the dialog it tells me that this is a png image which it isn't because i haven't changed the mime type so let me go ahead and change that to jpeg again and then once my server restarts i'll just try that again so now it tells me it's a jpeg image it tells me the size and so on and it tells me the programs i can open it with like photos or i can just save it so if you want to make the user download a file then you want to specify a file name if you want it to display in the browser then don't specify a file name and the only caveat to that is if it can't be displayed in the browser like a font file then it will automatically try to download so just keep that in mind when you're returning files and then the last thing i want to talk about is changing the description in the documentation so if i go to the docs here docs and we look at cad we see that the media type is application json which isn't necessarily correct you could still leave that there it won't affect anything but if you want to just change the documentation like changing the description i'll show you that as well so to do that it's set in the decorator and we're specifying responses right so in responses the first thing we need to worry about is the response code so here 200 is what i want for success and then i can specify a description so description and then i can say something like uh a picture of a cat right so if i reload the server and then refresh this we see a picture of a cat there and if i want to change the media type which really won't affect anything this is just documentation to help the user understand what's going on and to allow them to try things directly it really doesn't work well with files trying things out directly so i don't necessarily think you need to change any of this but if you want to you can so for example the media type if i want i can add a second thing to this dictionary here for 200. i can specify the content and then that takes in a dictionary the key of the dictionary is the mime type so image slash jpeg and then that has another dictionary and we can say example and then for this particular one i can say something like no example available just imagine a picture of a cat right so now the server restarts and i'll refresh and then i'll change to image slash jpeg here and then i see the example value that i just wrote no example available just imagine a picture of a cat so that's all i wanted to talk about with returning files from fast api as you can see it's fairly straightforward there are just some things that you may want to keep in mind when you're doing this so i'll post a link to the code in the description below i also post a link to that documentation for mime types if you want to look at that if you have any questions about anything i've done in this video feel free to leave a comment down below if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching and i will talk to you next time
Info
Channel: Pretty Printed
Views: 27,735
Rating: undefined out of 5
Keywords: fastapi, fastapi files, files in fastapi, return files, python, tutorial, mime types
Id: vpTAqnAbowo
Channel Id: undefined
Length: 10min 39sec (639 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.