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.
Example

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

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

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

The HTML script of index.html is given below.

<html>

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

hello.js contains sayHello() function.

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