Static Files – Flask

When you work for web development, you will need to use some files for javascript or CSS, here in FLASK when you need to add some static files, a special endpoint is used named as static. These static files are served from static folder in your package or next to your module and it will be available at /static in the application.

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

def index():
   return render_template("index.html")

if __name__ == '__main__': = True)

The HTML script of index.html is given below.


      <script type = "text/javascript" 
         src = "{{ url_for('static', filename = 'hello.js') }}" ></script>
      <input type = "button" onclick = "sayHello()" value = "Say Hello" />

hello.js contains sayHello() function.

function sayHello() {
   alert("Hello World")