How do I reference a file in a parent directory in HTML in my Flask app (NOT a static file)?

Viewed 129

Building my first basic Flask app as a UI page for some other Python scripts.

My directory structure is:

project
│   README.md  
│
└───app
│   │   test_script.py
│   │   flask.py
│   │
│   └───templates
│       │   index.html
│       │   other.html
│   
└───resources
    │   test1.txt
    │   test2.txt

My flask.py contains:

from flask import Flask, render_template
import test_script

app = Flask(__name__)

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

@app.route('/file<num>-update/')
def update_file(num):
    if num == '1':
        test_script.update_file1()
        return """
        <h1>File 1</h1>
        File 1 was updated.
        <embed src='../resources/test1.txt'>
        """
    elif num == '2':
        test_script.update_file2()
        return """
        <h1>File 2</h1>
        File 2 was updated.
        <embed src='../resources/test2.txt'>
        """
    else:
        return 'Invalid file selected.'

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

index.html is:

<!doctype html>

<head><title>Test</title></head>
    <meta charset=utf-8> </head>
    <body>
        <h1>My Website</h1>
        <form action="/file1-update/">
            <input type="submit" value="Update File 1" />
        </form>

        <form action="/file2-update/">
            <input type="submit" value="Update File 2" />
        </form>
    </body>

and test_script.py is irrelevant but simply appends a string with a date/time to the test file.

I want to be able to view the content of my files (test1.txt and test2.txt) within my HTML page. Just using <embed> to test with for now, but I think the method of displaying is irrelevant, because Flask can't seem to find the ../resources/test1.txt path. This also happens if I use a template for the HTML instead of writing it directly into the update_file(num) function. The rest of the code functions as intended. How do I fix my paths?

0 Answers
Related