Software Development

Learn File With out Saving in Flask

Learn File With out Saving in Flask
Written by admin


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Flask is a versatile, light-weight web-development framework constructed utilizing python. A Flask utility is a Python script that runs on an internet server, which listens to HTTP requests and returns responses. It’s designed for easy and quicker improvement. On this article we’ll focus on how can we Learn information with out Saving them in Flask, we will even confirm that the file uploaded efficiently or not.

Required Module

Set up Flask by operating the pip command in your terminal.

pip set up Flask

Implementation to Add and Learn the CSV File in Flask Software

Right here on this Flask utility, we’re making a folder named ‘Read_File’ to learn our information. Create a Digital surroundings, After making the digital surroundings we’ll create one templates folder during which we’ll write our HTML half and we additionally create the app.py file during which we’ll write our principal a part of the code in Flask Python.

File Construction:

Read file without Saving in Flask

 

index.html

On this instance, we’ll function the operation of the learn file with out saving it within the Flask.

HTML

<!DOCTYPE html>

<html>

    <head>

        <title>Learn File With out Saving in Flask</title>

    </head>

    <physique>

        <type technique="publish" enctype="multipart/form-data">

            <enter kind="file" title="file">

            <enter kind="submit" worth="Submit">

        </type>

    </physique>

</html>

Output:

Read file without Saving in Flask

 

app.py 

On this instance, the Flask utility has an endpoint on the root URL /. The endpoint will be accessed through the GET and POST strategies. When a consumer submits the shape with a picture file, the file is uploaded to the server through the POST technique, and its content material is learn utilizing the request.information.get(“file”) technique. The content material of the file is then encoded as a base64 string and included because the src attribute of an HTML img tag. This permits the picture to be displayed within the browser with out saving it to the server. When the endpoint is accessed through the GET technique, the index.html template is returned, which accommodates the shape for importing the file.

Python

from flask import Flask, request, render_template

  

app = Flask(__name__)

  

UPLOAD_FOLDER = 'static/uploads/'

  

@app.route("/", strategies=["GET", "POST"])

def index():

    if request.technique == "POST":

        file = request.information.get("file")

        file_content = file.learn()

          

        

        if file_content:

            return "Uploaded Profitable"

        else:

            return "Uploaded Unsuccessful"

  

    return render_template("index.html")

  

if __name__ == "__main__":

    app.run(debug=True)

Output:

Read file without Saving in Flask

 

Read file without Saving in Flask

When no file is uploaded 

Read file without Saving in Flask

When a file is uploaded 

About the author

admin

Leave a Comment