Error Reading CSV file using Python, Flask - python

I'm very new to python and flask. I simply wanted to read a CSV file but it's giving me an error of "FileNotFoundError: [Errno 2] No such file or directory: 'Dog-Data.csv'" everytime I try to run run.py
Here are my file order
DD\
static\
(bunch of image files)
templates\
(bunch of template files)
__init__.py
views.py
Dog-Data.csv
views.py
from flask import render_template
from app import app
import csv
#app.route('/')
#app.route('/Home')
def home():
return render_template("Home.html",title='Home')
#app.route('/MakeaMatch')
def makeamatch():
return render_template("MakeaMatch.html",title='Make a Match!')
#app.route('/Game')
def game():
return render_template("Game.html",title='Game')
#app.route('/ListofDogs')
def listofdogs():
return render_template("ListofDogs.html",title='List of Dogs')
#app.route('/About')
def about():
return render_template("About.html", title='About')
#app.route('/Contact')
def contact():
return render_template("Contact.html", title='Contact')
#app.route('/MatchResult')
def matchresult():
class DogBreed:
def __init__(self, br, per, si, lif, hou, cli):
self.breed = br
self.personality = per
self.size = si
self.lifestyle = lif
self.housing = hou
self.climate = cli
self.match = 0
#List that will contain class DogBreed
ListOfBreeds = []
data_file = open('Dog-Data.csv')
csv_file = csv.reader(data_file)
for row in csv_file:
#print (row) #will print the csv file
#print (row[2]) #will print element of that row
dog_breed = DogBreed(row[0],row[1].lower().split(", "),row[2].lower(),row[3].lower(),row[4].lower(),row[5].lower())
ListOfBreeds.append(dog_breed)
data_file.close()
#MORE CODES HERE. OMITTED BECAUSE I DON'T THINK IT'S RELEVANT
return render_template("MatchResult.html",title='Match Result',posts=ListOfBreeds)
The webpage loads and templates shows up fine if I comment out the lines involving CSV. However, it doesn't of course show the result I want it too.
I've also tried putting the Dog-Data.csv into the static folder and used
data_file = open('static/Dog-Data.csv')
but this didn't work either.
Thanks so much for help.

Have you tried to give the full path instead of just a relative path?
Python sometimes takes the working directory as a "home" path, which might or might not be the same as your view.py is situated in.

ok so really simple mistake that took hours for me to solve.
I should be passing it from the root folder which means I should've put
data_file = open('app/Dog-Data.csv')
and now it works. T__T

Related

Flask app. FileNotFoundError: [Errno 2] No such file or directory

I am trying to create my first Flask app where I am getting user data, doing a vlookup(merge) to add a few columns to the user, then doing a prediction on the whole dataframe. The problem is I cannot read my CSV data saved in the static folder so as to do the merge. Flask cannot see the csv files. Any help will be highly appreciated:
Here is my code snippet
app = Flask(__name__, template_folder='templates')
def predict():
if request.method == 'POST':
with open("static/region_map.csv", "r") as f1:
region_map = f1.read()
FileNotFoundError: [Errno 2] No such file or directory: 'static/region_map.csv'
The values for paths set during configuration can be requested. These are absolute and should work. The static directory can be queried using the static_folder attribute.
import os
def predict():
if request.method == 'POST':
target = os.path.join(app.static_folder, 'region_map.csv')
with open(target) as fp:
region_map = fp.read()
# ...
The open function opens the file and returns a file object. If you read the content in text mode using read, you will in fact get a string back.
To get a dataframe from a csv file, it is advisable to use pandas.read_csv.
import os
import pandas as pd
def predict():
target = os.path.join(app.static_folder, 'region_map.csv')
df = pd.read_csv(target)
# ...

Returning an updated list on increase in number of files

I want to return the updated list with the new file name in the list without explicitly refreshing the server..
This is my backend code written in flask :-
#app.route("/list")
def return_list():
all_files = os.listdir(r'H:/JS/uploads/')
files_without_extension = []
for file in all_files:
temp = os.path.splitext(file)
if(temp[1] == ".json" ):
files_without_extension.append(temp[0].title())
print("Total number of files : {}".format(len(all_files)))
print(files_without_extension)
return jsonify(files_without_extension)
The problem is when I refresh my web page, the list is being updated. I want the list to be updated if there are files removed or added in the directory.
I think you can only achieve this with websockets.
An alternative solution is to update the page regularly. The user who deletes a file himself is redirected.
<meta http-equiv="refresh" content="5">
#app.route('/list')
def uploads_list():
def _froot(fname):
root,_ = os.path.splitext(fname)
return root
return jsonify(_froot(fname) for fname in glob.iglob('H:/JS/uploads/*.json'))
#app.route('/remove/<path:fname>', methods=['POST'])
def uploads_remove(fname):
if os.path.exists(f'{fname}.json'):
try:
os.remove(f'{fname}.json')
except OSError as err: pass
return redirect(url_for('uploads_list'))

is there any way of continously reading CSV file and show it on HTML using Flask?

I have written a small job that takes the data from CSV and show it on HTML using Flask. It works fine but I want to automate it in a way that it starts showing any change in CSV. The CSV file is getting data from live system which means that the file content is changing with time. From my code it only shows me only show me present file and does not update for the newer content.
I have tried running bat file that will start python script but the problem is once it is started, I need to start python again so that it takes latest CSV.
from flask import Flask,render_template
import tablib
app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'file.csv')) as f:
dataset.csv = f.read()
#app.route("/")
def index():
return dataset.html
if __name__ == "__main__":
app.run()
You need to read the file within your index function.
#app.route("/")
def index():
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'file.csv')) as f:
dataset.csv = f.read()
return dataset.html

How do I call an existing python file with multiple functions from a main.py Bottle

Currently, my code is like this where I upload 2 files but I need to process them in temp files via another existing parse.py file with multiple functions.
How can I call them in Templates.py?
I tried adding import parse.py but it would give an error.
templates.py
#route('/')
def index():
return template('index')
#route('/', method='POST')
def upload():
incfile = request.files.get('uploadinc')
datfile = request.files.get('uploadhex')
macro, ext1 = os.path.splitext(incfile.filename)
data, ext2 = os.path.splitext(datfile.filename)
if ext1 not in ('.txt'):
return 'File extension not allowed.'
if ext2 not in ('.txt'):
return 'File extension not allowed.'
incfile.filename = 'macro.txt'
datfile.filename = 'data.txt'
curr_dir = os.getcwd()
print(curr_dir)
temp_dir = os.path.join(curr_dir, r'temp01')
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)
incfile.save(temp_dir)
datfile.save(temp_dir)
clean_up(temp_dir) // gives error
#route('/')
def clean_up(): // gives error
parse.py
import os, sys, re, binascii
def clean_up():
if os.path.exists("dataparse.txt"):
os.remove("dataparse.txt")
else:
print("Creating new files...")
if os.path.exists("out3.txt"):
os.remove("out3.txt")
else:
print("Creating new files...")
def parse_hexdump():
a = open("data.txt","r")
b = open("dataparse.txt","a")
w = open("out3.txt","a")
str = a.readline()
w.write(str)
for line in a:
if line.startswith('MD') or line.startswith('END OF DISPLAY'):
continue
else:
strline = line[5:40:] # Slice lines from 5-40 to another file
b.write(strline+'\n')
b.close()
w.close()
Just import parse, you don't put .py at the end of an import statement. Since you seem to want to just use the functions rather than calling parse.clean_up, you could instead do from parse import clean_up. The file parse needs to either be in your local directory (where you're running the python interpreter) or in your PYTHONPATH environment variable.

Reading json in pythonanywhere flask app

First I have seen this question. My problem is I have a flask app running on pythonanywhere that reads info from a json file in the same directory on the server, and get the following error:
Internal Server Error:The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application..
I simplified the app down to:
from flask import Flask
import json
app = Flask(__name__)
#app.route('/')
#app.route('/index')
def index():
return 'Index'
#app.route('/courses')
def courses():
with open('courses.json', 'r') as f:
these_courses = json.load(f)
return str(these_courses)
If I go to the index page I see index, as expected, but if I try to go to /courses then I get the error.The whole things runs fine on localhost, then with the same code I get an error on the server, so I know reading from the file works fine. This makes me think it might be a problem unique to json combined with pythonanywhere.
Edit: Perhaps a problem with the path name for courses.json, but it's in the same directory so I feel like it should be fine, just a thought
Turns out it was a pathname problem. I guess on files need to be routed from the root directory.
I ran:
def courses():
my_dir = os.path.dirname(__file__)
json_file_path = os.path.join(my_dir, 'courses.json')
return json_file_path
to find the path, then changed the function to:
def courses():
with open('/home/username/path/to/file/courses.json', 'r') as f:
these_courses = json.load(f)
return str(these_courses)
and now it worked :D
Then to make a better version that doesn't break when you move the project I did it like this:
def courses():
my_dir = os.path.dirname(__file__)
json_file_path = os.path.join(my_dir, 'courses.json')
with open(json_file_path, 'r') as f:
these_courses = json.load(f)
return str(these_courses)
As an alternative:
import pathlib
path = pathlib.Path('courses.json').absolute()
these_courses = json.loads(path.read_text())

Categories

Resources