Return Files with send_file - Flask Web Development with Python 30
Skills:
API Design70%
Key Takeaways
This video demonstrates how to return files with Flask's send_file function in a web development project, including examples with image and PDF files.
Full Transcript
what's going on everybody welcome to another flash tutorial video in this video what we're going to be talking about is how you can return files with flask so that's pretty simple so won't take us very long to get through this one so let's open up init.py and first of all in the Imports here from flask go ahead and import uh send file as well now we'll come on down to I this will be a good spot copy this paste and we're going to have a couple of functions here the first one is going to be the page that the user might visit like file downloads file downloads and this is going to return uh render template template and we'll just have this be downloads. HTML and then we'll have another function that will be uh return files or return file that's fine um and then return file and instead of returning a template this returns a send file now um we'll just leave that empty for now and first let's go ahead and and get this downloads. HTML done so let's go over here and um as usual let's just duplicate the 404 and we'll call it downloads. HTML open that up and let's start cleaning this get rid of all that and then basically div to div here that's good enough okay so then within this file uh we're going to add a little download button so it'll be we'll have it actually link here to slash return file uh and we'll make the target a blank tab and then in here button class equals and this will be a BTN bn- default button slash button bong and between that we'll just have it say download okay so that's a good enough downloads we'll go ahead save that and good enough and then let's just pass a file in here so going back over here into static let's get some files so this is just a little image of python so python to jpg feel free to use whatever image you have at your disposal and then let's just give the full path to that image so that would just you can double click here and grab at least the starting path so send file and so you can send to that and then it was just python. jpg G now you can get away with just doing that and optionally you can have uh basically like a file name so you can say um you know attachment attachment file name equals python. jpg something like that is fine so we can save that and let's close out of here and let's uh reload there and we'll bring over the website and what was that download files I think download files maybe no that's not the name what was the name of our thing file downloads all right file Dash downloads all right so here we are and then we have the download button opens a new tab and sure enough what's return is just the image for us so we can move that aside and uh just for another example I've got a real short PDF here um oh hey. PDF and uh the return file here will be uh we can just call this oh. PDF come over here change this to oh. PDF save oh looks like oh I guess I never put that in and then let's try again there we go and let's come over back to our website uh let's refresh and click on that a we're still returning that let's see we might be cached here yeah sure enough we were cashed if you're cashed uh shift F5 if you're in Chrome anyway here's our awesome PDF um and important most importantly it acts as a PDF and if you were to go to you know save it and whatnot it would be saved as a PDF okay so anyway that's pretty simple the send file is pretty robust you don't really need to do too much else when you're doing like working with it so that's pretty nice now the next tutorial that we're going to be talking about is what happens if you want to like protect a file so maybe only certain users can access that file so with pythonprogramming.net for example I let the users just download the tutorial videos but you know you could make it so easily so the user can't access the page to get to the download button but the download would almost certainly have to reside within the static directory so stack director is public to everybody so that's no good all right so the next tutorial we'll be talking about how you can make a protected directory that is not accessible to the public and is only accessible to uh people or in scenarios that you specifically specify so uh that's what we'll be talking about in the next tutorial if you have questions comments on this tutorial feel free to leave below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
In this Flask Web development tutorial, we're going to be discussing how to return files rather than templates.
Sample code and text-based tutorial: http://pythonprogramming.net/flask-send-file-tutorial/
http://pythonprogramming.net
https://twitter.com/sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: API Design
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
🎓
Tutor Explanation
DeepCamp AI