-1

I have written an python program, which scrapes the data from web and saves it as a .csv file. I want to show the .csv file in a HTML page using flask, but after trying it so many times, my with open file doesn't find the location of the .csv file.

My Project Structure looks like this:

├── main.py
├── scraping
│   ├── exports-imports.csv
├── website
│   ├── __init__.py
│   ├── templates
│   │   ├── datas.html
│   └── datas.py

main.py:

from website import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True, port=9000)

init.py:

from flask import Flask

def create_app():
    app = Flask(__name__)

    app.debug = True

    from .datas import datas

    app.register_blueprint(datas, url_prefix='/')

    return app

datas.html:

<table>
    <tr>
        <thead>
        <th>Year</th>
        <th>Exports</th>
        <th>Imports</th>
        </thead>
    </tr>
    {% for row in data %}
    <tr>
        <td>{{ row[0] }}</td>
        <td>{{ row[1] }}</td>
        <td>{{ row[2] }}</td>
    </tr>
    {% endfor %}
</table>

***exports-imports.csv ***:

1964;33193;30084
1965;36635;36019
1966;41224;37156
1967;44505;35884
1968;50900;41506
1969;58061;50092

datas.py:

from flask import Blueprint, render_template
import csv

datas = Blueprint('datas', __name__)


@datas.route('/datas')
def show_data():
    try:
        data = []
        with open('../scraping/exports-imports.csv', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                data.append(row)
        return render_template('datas.html', data=data)
    except FileNotFoundError:
        return "Error: file not found"

I have tried changing the location of the exports-imports.csv file all around my project, but still the flask cannot detect where it is situated, even though when I use Ctrl + Mouse Right Click leads me to the source csv file. I have searched in the internet, and everybody is doing the same, but it has not worked for me. I hope someone here could help me. If possible without using pandas and any other libraries.

2 Answers2

0

The following example uses the absolute path to the application's parent directory as the base to add the relative path specification to the file.

from flask import Blueprint, current_app, render_template
import csv, os 

datas = Blueprint('datas', __name__)

@datas.route('/datas')
def show_data():
    try:
        data = []
        path = os.path.join(
            current_app.root_path, os.pardir, 
            'scraping', 'exports-imports.csv'
        )
        with open(path, 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                data.append(row)
        return render_template('datas.html', data=data)
    except FileNotFoundError:
        return "Error: file not found"
Detlef
  • 6,137
  • 2
  • 6
  • 24
0

Try to use an absolute path (instead of a relative path) in order to open the csv file

from pathlib import Path

with open(Path(__file__).parent.absolute() / Path('../scraping/exports-imports.csv'), 'r') as f:
        reader = csv.reader(f)
youvann
  • 1
  • 1