0

I'm using Flask and Python for the first time of my life. I want to display a HTML page with some CSS and JavaScript files. I found that for CSS the best way was to set up my server using static route

from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import socket, json


# create Flask app
app = Flask(__name__, static_url_path='.')

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

app = Flask(__name__, )

@app.route('/static/<path:path>')
def send_static(path):
    return send_from_directory('static', path)

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

So into the folder /static I have /static/css/xx.css and /static/js/xx.js, I figured out that I had to make the relation to my CSS file like this in the HTML

<link rel="stylesheet" href="{{ url_for('static', filename='css/bulma.min.css') }}">

Now I have the same struggle to link my JavaScript files. In general I import them like this

<script src="/static/js/fetch.umd.js"></script>
<script src="/static/js/app.js"></script>

But for a unkown reason it doesn't work and I think it's because I need to import them with a special way that I don't know yet.

Jérôme Vial
  • 15
  • 1
  • 5

1 Answers1

2

You should be importing them the same way as you import your CSS.

For example:

<script src="{{ url_for('static', filename='static/js/app.js') }}"></script>
jaredad7
  • 998
  • 2
  • 11
  • 33