18

I am trying to serve a static html file, but returns a 500 error (a copy of editor.html is on .py and templates directory) This is all I have tried:

from flask import Flask
app = Flask(__name__, static_url_path='/templates')
@app.route('/')
def hello_world():
    #return 'Hello World1!' #this works correctly!
    #return render_template('editor.html')
    #return render_template('/editor.html')
    #return render_template(url_for('templates', filename='editor.html'))
    #return app.send_static_file('editor.html') #404 error (Not Found)
    return send_from_directory('templates', 'editor.html')

This is the response:

Title: 500 Internal Server Srror

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.
shuji
  • 7,369
  • 7
  • 34
  • 49

4 Answers4

34

Reducing this to the simplest method that'll work:

  1. Put static assets into your static subfolder.
  2. Leave Flask set to the default, don't give it a static_url_path either.
  3. Access static content over the pre-configured /static/ to verify the file works

If you then still want to reuse a static file, use current_app.send_static_file(), and do not use leading / slashes:

from flask import Flask, current_app
app = Flask(__name__)

@app.route('/')
def hello_world():
    return current_app.send_static_file('editor.html')

This looks for the file editor.html directly inside the static folder.

This presumes that you saved the above file in a folder that has a static subfolder with a file editor.html inside that subfolder.

Some further notes:

  • static_url_path changes the URL static files are available at, not the location on the filesystem used to load the data from.
  • render_template() assumes your file is a Jinja2 template; if it is really just a static file then that is overkill and can lead to errors if there is actual executable syntax in that file that has errors or is missing context.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So, what if this is an actual template? how should I call it? – shuji Jul 04 '14 at 17:08
  • 1
    @shuji: then you put it in the `templates` subdirectory and use `return render_template('editor.html')`. Make sure you import the `render_template` function. – Martijn Pieters Jul 04 '14 at 17:16
  • @MartijnPieters I am unable to add my css, js and images to this html file. How should I be storing my helper files so that they get picked up the server? – Amriteya Mar 07 '17 at 11:01
  • @Amriteya: why not make the file a proper template and serve it from a new route? That way you can use the standard templating functionality to include static resources like JS, CSS, or images. Otherwise, check your browser development tools to see where it is loading those resources from, and adjust as needed. Without details about your situation, I can't offer anything else but generic advice, and comments are not the place for problem solving like this. – Martijn Pieters Mar 07 '17 at 11:07
  • @MartijnPieters It is not loading in my browser under the resources tab. The fact is I have a single HTML and one js, css and image each supporting that HTML. Templating seems pointless for this much. – Amriteya Mar 07 '17 at 11:13
  • What's the performance gain of serving a static file as opposed to rendering a template? – Fabien Snauwaert Mar 05 '23 at 21:02
  • 1
    @FabienSnauwaert: how long is a piece of string? Loading and parsing the template into an executable structure can be cached, but executing the template takes a non-trivial amount of work, how much depends on what's in the template and what else that calls out to. Serving the file can actually be delegated to the fronting HTTP server. So, on average it'll probably be faster but I can't tell you by how much. Measure it. – Martijn Pieters Mar 06 '23 at 07:11
5

All the answers are good but what worked well for me is just using the simple function send_file from Flask. This works well when you just need to send an html file as response when host:port/ApiName will show the output of the file in browser


@app.route('/ApiName')
def ApiFunc():
    try:
        #return send_file('relAdmin/login.html')
        return send_file('some-other-directory-than-root/your-file.extension')
    except Exception as e:
        logging.info(e.args[0])```

Binoy S Kumar
  • 289
  • 4
  • 9
0

send_from_directory and send_file need to be imported from flask.

Your code sample would work if you do the following:

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

@app.route('/')
def hello_world():
    return send_from_directory('templates', 'editor.html')

However, remember if this file loads other files e.g. javascript, css, etc. you would have to define routes for them too.

Also, as far as I understand, this is not the recommended method on production because it's slow.

Danyal
  • 528
  • 1
  • 7
  • 17
0
from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')

@app.route('/')
def hello_world():
    return render_template('editor.html')
RateRebriduo
  • 265
  • 3
  • 11