{"id":931,"date":"2022-12-10T13:51:00","date_gmt":"2022-12-10T08:21:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=931"},"modified":"2023-08-15T15:31:33","modified_gmt":"2023-08-15T10:01:33","slug":"render-images-from-flask","status":"publish","type":"post","link":"https:\/\/geekpython.in\/render-images-from-flask","title":{"rendered":"Upload and Display Images On the Frontend Using Python Flask"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"heading-introduction\">Introduction<\/h1>\n\n\n\n<p>Flask is a micro web framework written in Python for web development using Python. We can do almost everything in Flask by writing Python code as we do in JavaScript for making an application.<\/p>\n\n\n\n<p>But things get a little tricky and tedious when handling the files or images in Flask. Displaying the local or static files is a lot easier than handling the process of uploading and displaying the dynamic files.<\/p>\n\n\n\n<p>In this article, you&#8217;ll learn the methods for handling the process of displaying and uploading images in Flask.<\/p>\n\n\n\n<p>This article will cover two segments &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Displaying local images<\/strong> &#8211; Since local images are already present in our storage system, so you&#8217;ll see the methods for displaying them on the client side.<\/li>\n\n\n\n<li><strong>Uploading and displaying the dynamic images<\/strong> &#8211; In this segment, you&#8217;ll see the process of uploading the image to your specified folder and then displaying that uploaded image from the folder.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-pre-requisites\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-pre-requisites\"><\/a>Pre-requisites<\/h1>\n\n\n\n<p><strong>Python<\/strong>,&nbsp;<strong>Flask,<\/strong>&nbsp;and&nbsp;<strong>Jinja<\/strong>&nbsp;have been used in this article, so you should have a basic knowledge of all of them because Python has been used to write the code and Jinja has also been used in some other parts.<\/p>\n\n\n\n<p>Don&#8217;t worry if you don&#8217;t understand&nbsp;<strong>Flask<\/strong>&nbsp;and J<strong>inja templating<\/strong>&nbsp;because anyway, I will explain all the methods in detail.<\/p>\n\n\n\n<p>Let&#8217;s begin with the first segment.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-displaying-local-images\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-displaying-local-images\"><\/a>Displaying local images<\/h1>\n\n\n\n<p>As mentioned earlier, it is easy to render local images in Flask because all the images are served from the&nbsp;<strong>static<\/strong>&nbsp;folder of the project directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-urlfor-method\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-using-urlfor-method\"><\/a>Using&nbsp;url_for&nbsp;method<\/h2>\n\n\n\n<p>First, you will create a directory, and then inside it create the folder structure shown below.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">flask_directory\n\u251c\u2500\u2500 app.py\n\u251c\u2500\u2500 static\n\u2502   \u2514\u2500\u2500 Image\n\u2502       \u2514\u2500\u2500 sample.png\n\u2514\u2500\u2500 templates\n    \u2514\u2500\u2500 img_render.html<\/pre><\/div>\n\n\n\n<p>After the complete folder structure is created, the code to start the flask server and render the HTML template needs to be written in the&nbsp;<code>app.py<\/code>&nbsp;file. Please copy the code written below and paste it into your&nbsp;<code>app.py<\/code>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">from flask import Flask, render_template\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef home():\n    return render_template('image_render.html')\n\nif __name__ == '__main__':\n    app.run(debug=True, port=9000)<\/pre><\/div>\n\n\n\n<p>In this code, we first imported&nbsp;<code>Flask<\/code>&nbsp;and&nbsp;<code>render_template<\/code>&nbsp;then instantiated the app then after that created a route that will render our HTML template and finally, there is the driver code that will run the app.<\/p>\n\n\n\n<p>Now we have to add HTML to our&nbsp;<code>img_render.html<\/code>&nbsp;file and also write the logic to render the image. With the help of&nbsp;<strong>Jinja templating<\/strong>&nbsp;and&nbsp;<code>url_for()<\/code>, images present in the&nbsp;<code>static<\/code>&nbsp;folder can be rendered.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Rendering Local Images Using Flask&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;img src= \"{{url_for('static', filename='\/Image\/GP.png')}}\" &gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>Basic HTML code is written in the above HTML file and&nbsp;<code>\"{{ url_for('static', filename='\/Image\/GP.png')}}\"<\/code>&nbsp;is used in the&nbsp;<code>src<\/code>&nbsp;attribute inside the&nbsp;<code>img<\/code>&nbsp;tag. This will find and render our images in the&nbsp;<strong>static<\/strong>&nbsp;folder. And this code is surrounded by&nbsp;<code>{{ }}<\/code>&nbsp;which is a convention for writing&nbsp;<strong>Jinja<\/strong>.<\/p>\n\n\n\n<p>If you run your app now, you will see your image displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"572\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-5.png\" alt=\"Image displayed using url_for() method\" class=\"wp-image-933\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-5.png 568w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-5-298x300.png 298w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/1-5-150x150.png 150w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-variable\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-using-variable\"><\/a>Using variable<\/h2>\n\n\n\n<p>Apart from the method shown above, you can also use another method. In this method, by storing the image in the&nbsp;<strong>variable<\/strong>, you can display the image by inserting that variable in the&nbsp;<code>img<\/code>&nbsp;tag in the HTML file.<\/p>\n\n\n\n<p>Again, you create the same folder structure, and this time, you will see a different approach.<\/p>\n\n\n\n<p>This time copy the code given below in your&nbsp;<code>app.py<\/code>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">from flask import Flask, render_template\nimport os\n\napp = Flask(__name__)\n\nimg = os.path.join('static', 'Image')\n\n@app.route('\/')\ndef home():\n    file = os.path.join(img, 'GP.png')\n    return render_template('image_render.html', image=file)\n\nif __name__ == '__main__':\n    app.run(debug=True)<\/pre><\/div>\n\n\n\n<p>In the above code we have done all the same as before but this time, we first define the path of the image folder and then joined the image folder path and image inside the&nbsp;<code>home<\/code>&nbsp;function and stored it in the&nbsp;<code>file<\/code>&nbsp;variable and then passed it to the&nbsp;<code>render_template<\/code>&nbsp;along with the HTML template.<\/p>\n\n\n\n<p>And to display the image, the&nbsp;<code>image<\/code>&nbsp;variable has to be sent to the HTML template.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Rendering Local Images Using Flask&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;img src= \"{{image}}\" &gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>If you run the&nbsp;<code>app.py<\/code>&nbsp;file, you will see the image displayed on the client side.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"603\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-6.png\" alt=\"Image rendered using variable method\" class=\"wp-image-934\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-6.png 517w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/2-6-257x300.png 257w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/figure>\n\n\n\n<p>Now let&#8217;s move on to the next segment of the article, where you will learn to upload and display the dynamic images.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-upload-and-display-dynamic-images\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-upload-and-display-dynamic-images\"><\/a>Upload and display dynamic images<\/h1>\n\n\n\n<p>Getting dynamic images displayed is a bit difficult. First, the photos have to be uploaded to the local folder and then that image has to be served from the uploaded folder. And in this process, more code has to be written.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-a-basic-approach\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-a-basic-approach\"><\/a>A basic approach<\/h2>\n\n\n\n<p>In this method, you will see the most basic approach which is preferred by most developers.<\/p>\n\n\n\n<p>As you saw in the first segment, we used the&nbsp;<strong><em>variable<\/em><\/strong>&nbsp;method to render the local image. Keeping this in mind, we will use the&nbsp;<strong>variable<\/strong>&nbsp;in this method too, but the process will be slightly different.<\/p>\n\n\n\n<p>Again create the same folder structure as before and then copy the code given below in the&nbsp;<code>app.py<\/code>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">from flask import Flask, render_template, request\nfrom werkzeug.utils import secure_filename\nimport os\n\napp = Flask(__name__)\n\nupload_folder = os.path.join('static', 'uploads')\n\napp.config['UPLOAD'] = upload_folder\n\n@app.route('\/', methods=['GET', 'POST'])\ndef upload_file():\n    if request.method == 'POST':\n        file = request.files['img']\n        filename = secure_filename(file.filename)\n        file.save(os.path.join(app.config['UPLOAD'], filename))\n        img = os.path.join(app.config['UPLOAD'], filename)\n        return render_template('image_render.html', img=img)\n    return render_template('image_render.html')\n\n\nif __name__ == '__main__':\n    app.run(debug=True, port=8001)<\/pre><\/div>\n\n\n\n<p>First, we imported the required libraries and defined and configured the path to store the uploaded images.<\/p>\n\n\n\n<p>Then we defined a route that would handle the post request and defined a&nbsp;<code>upload_file<\/code>&nbsp;function to save and display the image.<\/p>\n\n\n\n<p>In the&nbsp;<code>upload_file<\/code>&nbsp;function, we took user input and then saved it in our&nbsp;<strong>upload<\/strong>&nbsp;folder path and then stored that uploaded input in the&nbsp;<code>img<\/code>&nbsp;variable and sent it to our HTML template, and finally rendered the HTML template.<\/p>\n\n\n\n<p>Now you must be wondering why we used&nbsp;<code>secure_filename<\/code>&nbsp;here. This ensures that what the user is inputting is secure and should always be used to secure the filename.<\/p>\n\n\n\n<p>After this, now you have to copy the code given below in the&nbsp;<code>image_render.html<\/code>&nbsp;file in the&nbsp;<strong>templates<\/strong>&nbsp;folder.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"image_render.html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Rendering Dynamic Images Using Flask&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;form action=\"{{ url_for('upload_file') }}\" enctype=\"multipart\/form-data\" method=\"POST\"&gt;\n    &lt;input name=\"img\" type=\"file\"\/&gt;\n    &lt;input type=\"submit\"\/&gt;\n&lt;\/form&gt;\n\n{% if img %}\n&lt;h1&gt;Rendered Image&lt;\/h1&gt;\n&lt;img src=\"{{img}}\"&gt;\n{% else %}\n&lt;h1&gt;Image will be render here...&lt;\/h1&gt;\n{% endif %}\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>Now in this code, we have inserted a simple form tag that handles the&nbsp;<strong>POST<\/strong>&nbsp;request which comes from the&nbsp;<code>upload_file<\/code>&nbsp;function and then inside it we have inserted two input tags one helps to choose the file and the other handles the submission.<\/p>\n\n\n\n<p>And further, in this code, we have rendered the image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"465\" height=\"191\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-3.png\" alt=\"App preview for the dynamic image\" class=\"wp-image-935\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-3.png 465w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/3-3-300x123.png 300w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/figure>\n\n\n\n<p><strong><em>The above image shown is before rendering the dynamic image from the user.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"559\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-3.png\" alt=\"After uploading and displaying the dynamic images\" class=\"wp-image-936\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-3.png 445w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/4-3-239x300.png 239w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<p><strong><em>The above image shown is after uploading the dynamic image from the user.<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-different-method\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-using-different-method\"><\/a>Using different method<\/h2>\n\n\n\n<p>In the earlier approach, you saw that we were storing the image in the variable and sending it to the HTML template, and then displaying the image from there.<\/p>\n\n\n\n<p>Now in this method, instead of using the variable method, we will use the&nbsp;<code>send_from_directory<\/code>&nbsp;method of Flask.<\/p>\n\n\n\n<p>Create the same folder structure and copy the below code in&nbsp;<code>app.py<\/code>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">from flask import Flask, request, render_template, send_from_directory\nfrom werkzeug.utils import secure_filename\nimport os\n\napp = Flask(__name__)\n\nupload = 'D:\/SACHIN\/Pycharm\/Image rendering\/Dynamic Img Render\/uploads'\n\napp.config['UPLOAD'] = upload\n\n@app.route('\/', methods=['GET', 'POST'])\ndef file_upload():\n    if request.method == 'POST':\n        f = request.files['file']\n        filename = secure_filename(f.filename)\n        f.save(os.path.join(app.config['UPLOAD'], filename))\n        return send_from_directory(app.config['UPLOAD'], filename)\n    return render_template('render.html')\n\n\nif __name__ == '__main__':\n    app.run(debug=True, port=4339)<\/pre><\/div>\n\n\n\n<p>We have written almost the same code which was written earlier in the method, the only difference is that this time instead of storing the image in a variable, we are rendering it directly from our upload folder with the help of&nbsp;<code>send_from_directory<\/code>&nbsp;method of Flask.<\/p>\n\n\n\n<p>This time our HTML template will have only one form with two input tags. Create a&nbsp;<code>render.html<\/code>&nbsp;file inside the templates folder and copy the code below.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"render.html\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Uploading a dynamic image&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;form action = \"\/\" method = \"post\" enctype=\"multipart\/form-data\"&gt;\n        &lt;input type=\"file\" name=\"file\" \/&gt;\n        &lt;input type = \"submit\" value=\"Upload\"&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>If you run the app, the selected image will be uploaded to the upload folder and then displayed on the client side with the help of&nbsp;<code>send_from_directory<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"493\" height=\"129\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/5-3.png\" alt=\"uploading image using send_from_directory method\" class=\"wp-image-937\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/5-3.png 493w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/5-3-300x78.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/figure>\n\n\n\n<p><strong><em>The above image shown is before uploading and displaying the specified image.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"451\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/6-2-1024x451.png\" alt=\"image rendering using send_from_directory method\" class=\"wp-image-938\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/6-2-1024x451.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/6-2-300x132.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/6-2-768x338.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/6-2.png 1365w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong><em>The above image shown is after uploading and displaying the specified image.<\/em><\/strong><\/p>\n\n\n\n<p>You can perform this method with a different approach. In this approach, you can create a separate route to display the image. Here&#8217;s how you can do it.<\/p>\n\n\n\n<p>The process will be the same as in the previous approach, but in this first, you will save the image and store it in a variable, then by defining a specific URL you will display the image with the help of the&nbsp;<code>send_from_directory<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import os\nfrom flask import Flask, render_template, request, redirect, url_for, send_from_directory\nfrom werkzeug.utils import secure_filename\n\nUPLOAD_FOLDER = 'D:\/SACHIN\/Pycharm\/Image rendering\/Image_Rendering'\n\napp = Flask(__name__)\napp.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER\n\n@app.route('\/', methods=['GET', 'POST'])\ndef upload_file():\n    if request.method == 'POST':\n        file = request.files['file']\n        filename = secure_filename(file.filename)\n        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))\n        return redirect(url_for('download_file', name=filename))\n    return render_template('upload.html')\n\n@app.route('\/uploads\/&lt;name&gt;')\ndef display_file(name):\n    return send_from_directory(app.config[\"UPLOAD_FOLDER\"], name)\n\n\nif __name__ == '__main__':\n    app.run(debug=True, port=6001)<\/pre><\/div>\n\n\n\n<p>Here we are doing the same but this time we have created a new route that will normally be used to display the image and in our&nbsp;<code>upload_file<\/code>&nbsp;function we have used the&nbsp;<code>redirect<\/code>&nbsp;function which will redirect to the specified route.<\/p>\n\n\n\n<p>Create a file named&nbsp;<code>upload.html<\/code>&nbsp;inside the&nbsp;<strong>templates<\/strong>&nbsp;folder just like you&#8217;ve done every time and then copy the below code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"upload.html\">&lt;!doctype html&gt;\n&lt;html&gt;\n&lt;title&gt;Upload new File&lt;\/title&gt;\n&lt;body&gt;\n&lt;h1&gt;Upload new File&lt;\/h1&gt;\n&lt;form enctype=\"multipart\/form-data\" method=\"post\"&gt;\n    &lt;input name=file type=file&gt;\n    &lt;input type=submit value=Upload&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>If you will run the app and try to upload the image then the uploaded image will be displayed on a different route.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"430\" height=\"206\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/7-1.png\" alt=\"uploading dynamic images\" class=\"wp-image-939\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/7-1.png 430w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/7-1-300x144.png 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"437\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/8-2-1024x437.png\" alt=\"Displaying the upload image on a different route\" class=\"wp-image-940\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/8-2-1024x437.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/8-2-300x128.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/8-2-768x328.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/8-2.png 1365w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/render-images-from-flask#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>Uploading an image and then displaying it is a bit tricky and in this article, you saw how to upload and display an image in Flask.<\/p>\n\n\n\n<p>In this article, you saw how to render static images and how to upload and display dynamic images.<\/p>\n\n\n\n<p>You saw two methods for rendering static files and two methods for uploading and displaying dynamic files.<\/p>\n\n\n\n<p>With the help of this article, you must have got an idea of how to handle static and dynamic files with Flask and also cleared some concepts of Flask. Some of these methods you may already know but some methods you may not know.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might like if you like this article<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/data-augmentation-in-deep-learning\" rel=\"noreferrer noopener\">An intuitive guide on data augmentation along with the techniques<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\" rel=\"noreferrer noopener\">Implement a custom deep learning model into the Flask app for image recognition<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/argparse-in-python\" rel=\"noreferrer noopener\">Build your first command line interface using Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/exec-function-in-python\" rel=\"noreferrer noopener\">Learn how to execute the dynamically generated code using Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/merging-tailwindcss-into-flask-apps\" rel=\"noreferrer noopener\">Make attractive web pages using Python by integrating TailwindCSS into the Flask<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-virtual-environments-venv\" rel=\"noreferrer noopener\">Python virtual environments: best practices and hands-on guide<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/web-scraping-in-python-using-beautifulsoup\" rel=\"noreferrer noopener\">Extract information from the web pages using Python and BeautifulSoup<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Flask is a micro web framework written in Python for web development using Python. We can do almost everything in Flask by writing Python code as we do in JavaScript for making an application. But things get a little tricky and tedious when handling the files or images in Flask. Displaying the local or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":284,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,5],"tags":[36,12,31],"class_list":["post-931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-flask","tag-flask","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=931"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/931\/revisions\/1336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/284"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}