0

I have a Flask server like:

from flask import Flask
app = Flask(__name__, static_url_path='')

@app.route('/')
def server_index():
    return app.send_static_file('ngrok_run_a_public_server.html')


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

I have it in a folder like:

examples
  -ngrok_run_a_public_server.html
  -server.py

They are both in the same folder, and this is how I saw other people serve static files. I am getting 404 both locally and on ngrok. I just want to server a file publicly.

It fails using both http://127.0.0.1:5000/ and http://127.0.0.1:5000.

halfer
  • 19,824
  • 17
  • 99
  • 186
codyc4321
  • 9,014
  • 22
  • 92
  • 165

2 Answers2

1

Just create a folder named static inside the examples folder and move your html file in there. It should work then.

Hari
  • 5,057
  • 9
  • 41
  • 51
1

You may create a folder in the root directory, any name you want - let's say 'myDailyReports'. And the file in this folder be say 'Day1.xlsx'.

import os
from flask import send_from_directory, current_app

@app.route('/')
def server_index():    

    path = os.path.join(current_app.root_path, 'myDailyReports')
    filename = 'Day1.xlsx'

    # returns the file to download as an attachment with as_attachment as True
    return send_from_directory(directory=path, filename=filename, as_attachment=True)

If you do not want to download, but just want to display the file contents on the browser, maybe a .txt or .html file -

return send_from_directory(directory=path, filename=filename)
Ejaz
  • 1,504
  • 3
  • 25
  • 51