0

I'm new to flask. I'm trying to take user import and save it in the backend. I failed to save the file (I couldn't see uploaded file in static), hence couldn't read from file. Can anyone suggest a solution? Thanks.

Error: FileNotFoundError: [Errno 2] No such file or directory: 'static/uploadsDB/Reviews.txt'

My app.py:

#flask packages
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap 
from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL

#systems
import os 
from werkzeug.utils import secure_filename
import spacy

app = Flask(__name__)
Bootstrap(app)
#db = SQLAlchemy(app)

# Configuration for File Uploads
files = UploadSet('files',ALL)
app.config['UPLOADED_FILES_DEST'] = 'static/uploadsDB'
configure_uploads(app,files)

# Spacy packages
nlp = spacy.load("en_core_web_sm")

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST','GET'])
def upload():
    if request.method == 'POST' and 'txt_data' in request.files:
        num_sentences = request.form['num_sentences']
        file = request.files['txt_data']
        filename = secure_filename(file.filename)
        # os.path.join is used so that paths work in every operating system
        # file.save(os.path.join("wherever","you","want",filename))
        file.save(os.path.join('static/uploadsDB',filename))
        fullfile = os.path.join('static/uploadsDB',filename)
        text_file = open(os.path.join('static/uploadsDB',filename), "r").read()
        nlp_file_doc = nlp(text_file)
        sentences = list(nlp_file_doc.sents)
        return render_template('annotation.html', sentences = sentences)

My index.html:

<div class="container">
          <div class="main">
              <div class="row">
                <div class="col-md-8" style="background-color:#ecb164">
                  <h3 style="color:#fff">Step 1: Upload your dataset</h3>
                    <p style="color:#fff">This tool only accept <ins>txt</ins> format text data </p>
                    <p style="color:#fff">Every sentence should either separate with punctuation or  </p>
                      <form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}" class="form-group">
                        <div class="form-group">
                          <input type="file" name="txt_data">
                          <h3 style="color:rgb(255, 255, 255)">Step 2: Input lines of sentences you have processed</h3>
                            <p style="color:#fff"> Please enter 0 if this is your first attempt </p>
                        <form method=POST action="{{ url_for('upload') }}" class="form-group">
                        <input type="text" name="num_sentences" value=0>
louis lau
  • 259
  • 2
  • 3
  • 5

1 Answers1

0

The problem is that the file does not get saved in the first place.

This is the problematic line:

file.save(os.path.join('static/uploadsDB',filename))

Here, file refers to the form data, but as you use Flask-Uploads or its successor Flask-Reuploaded the UploadSet is taking care of saving a file.

And you named your UploadSet files.

Also, as you have configured the path were to save the files already ( app.config['UPLOADED_FILES_DEST'] = 'static/uploadsDB' ) you do not need to repeat this information again.

So, in order to save the file, you only have to use the following code:

files.save(file)

The save method of an UploadSet also takes an argument name.

If you wish to give the saved file an arbitrary name, you can write ...

files.save(file, name="some_name")

If you want to learn more about Flask-Reuploaded you can always have a look at the source code.

You can also learn more about the used data structures for the form results in the werkzeug documentation.

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37