본문 바로가기

기초공부/WEB

[Flask]빠르게 보여주기 - 정적 파일

정적 파일(Static file)

static이라는 폴더를 생성한 패키지 아래에 만들거나 모듈옆에 위치시키면 개발된 어플리케이션에서 /static 위치에서 정적 파일을 제공한다.

url_for('static', filename='style.css')
아래는 이와 관련된 예시이다

#python 파일
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index() :
    return render_template("staticFile2.html") # staticFile2.html 을 렌더링한다

if __name__=='__main__' :
    app.run()
<!--html 파일-->
<!DOCTYPE html>
<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>
# 자바스크립트 파일
function sayHello(){
  alert("hello world!");
}


실제로 다음과 같은 결과가 나온다