1

I have a standalone app that takes in an excel file and outputs a word doc. This works fine as standalone.

I have now tried to integrate it into a Flask application, but flask can't find the subfolder "templates" of my application. Here is my file structure:

my_flask_site
├── flask_app.py
├── __init__.py    
├── templates
|   ├── index.html
|   └── report.html
├── uploads
|   └── myfile.xlsx
|  
└── apps
   └── convert_app
       ├── __init__.py
       ├── main.py
       ├── report
       |   ├── __init__.py
       |   ├── data_ingest.py
       |   └── report_output.py
       └── templates
           └── output_template.docx

now I can't get the report_output.py file to find the output_template.docx file now it is in the flask application.

def run_report(file):
    data = data_ingest.Incident(file)
    priority_count = dict(data.df_length())
    size = sum(priority_count.values())
    print(priority_count)
    print(size)

    report = report_output.Report()
    report.header()
    report.priority_header(0)
    i = 0    
    if '1' in priority_count:
        for _ in range(priority_count['1']):
            field = data.fields(i)
            report.priority_body(field)
            i += 1
            report.break_page()
        report.priority_header(1)
    else:
        report.none()
        report.priority_header(1)

    if '2' in priority_count:
        for _ in range(priority_count['2']):
            field = data.fields(i)
            report.priority_body(field)
            i += 1
            report.break_page()
        report.priority_header(2)
    else:
        report.none()
        report.break_page()
        report.priority_header(2)

    if '3' in priority_count:
        for _ in range(priority_count['3']):
            field = data.fields(i)
            report.priority_body(field)
            i += 1
            report.break_page()

    if '4' in priority_count:
        for _ in range(priority_count['4']):
            field = data.fields(i)
            i += 1
    output = OUTPUT_FILE+f"/Platform Control OTT Daily Report {data.field[0]}.docx"
    report.save(output)
    print(f"Report saved to:\n\n\t {output}")

def main(file):
    run_report(file)


if __name__ == "__main__":
    main()

and here is the report_output.py (without the word format part):

from docx import Document

class Report(object):
    def __init__(self):
        self.doc = Document('./templates/pcc_template.docx')

        self.p_title = ['Major Incident',
                    'Stability Incidents (HPI)',
                    'Other Incidents']
        self.date = datetime.now().strftime('%d %B %Y')


    def save(self, output):
        self.doc.save(output)

There is more in the format_report.py file, but it is related to the function of the app. Where I am stuck is how I get the app to again see it's own template folder and the template file inside it.

iFunction
  • 1,208
  • 5
  • 21
  • 35
  • If this is a duplicate question, where is the question that it duplicates please as that might have solved my problem. – iFunction Nov 29 '18 at 11:22

1 Answers1

1

I have solved my problem, after finding this post here Refering to a directory in a Flask app doesn't work unless the path is absolute.

What I take from this is that the file path has to be absolute from the Flask applications root folder, in this case "my_flask_site" is the root folder and adding the full file path solved the problem.

iFunction
  • 1,208
  • 5
  • 21
  • 35