Python Flask 프론트엔드 워크플로 설정

Python Flask 프론트엔드 워크플로 설정

2022-10-18 last update

24 minutes reading flask tailwindcss javascript python
안녕하십니까.! 나는 당신이 잘하고 있기를 바랍니다. 마지막 게시물에서 우리는 애플리케이션 백엔드 구조를 설정했고 이번 포스트에서는 애플리케이션에 대한 프론트엔드 워크플로를 설정합니다.

루트 폴더 구조에서 yarn init -y를 실행하여 원하는 원사 또는 npm을 설정하십시오.

yarn init -y


우리는 다음과 같은 파일package.json을 얻을 것입니다.

{
  "name": "flask_blog",
  "version": "1.0.0",
  "repository": "[email protected]:MuhammadSaim/flask_tutorial_blog.git",
  "author": "Muhammad Saim",
  "license": "MIT"
}


이제 이 명령을 사용하여 프로젝트에 대한 모든 Dev 종속성을 설치합니다. Laravel Mix을 빌드 도구로 사용할 것입니다.

yarn install autoprefixer laravel-mix postcss tailwindcss -D


이것을 실행하면 package.json 다음과 같이 보일 것입니다.

{
  "name": "flask_blog",
  "version": "1.0.0",
  "repository": "[email protected]:MuhammadSaim/flask_tutorial_blog.git",
  "author": "Muhammad Saim",
  "license": "MIT",
  "devDependencies": {
    "autoprefixer": "^10.4.8",
    "laravel-mix": "^6.0.49",
    "postcss": "^8.4.16",
    "tailwindcss": "^3.1.8"
  }
}


이제 프로젝트에 필요한 프로젝트의 종속성을 설치합니다. UI에 Tailwind의 UI 키트Daisy UI를 사용합니다.

yarn add @tailwindcss/typography daisyui jquery


이러한 종속성을 설치한 후 package.json는 다음과 같습니다.

{
  "name": "flask_blog",
  "version": "1.0.0",
  "repository": "[email protected]:MuhammadSaim/flask_tutorial_blog.git",
  "author": "Muhammad Saim",
  "license": "MIT",
  "devDependencies": {
    "autoprefixer": "^10.4.8",
    "laravel-mix": "^6.0.49",
    "postcss": "^8.4.16",
    "tailwindcss": "^3.1.8"
  },
  "dependencies": {
    "@tailwindcss/typography": "^0.5.4",
    "daisyui": "^2.22.0",
    "jquery": "^3.6.0"
  }
}


이제 프로젝트에 대한 tailwindcss 구성 파일을 생성합니다.

npx tailwindcss init


이것은 다음과 같은 tailwind.confg.js를 생성합니다.

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}


이제 루트 디렉터리에 src 폴더를 만들고 src 폴더 cssjs에 두 개의 폴더를 더 만들고 app.css 폴더에 css 파일을 만들고 app.js 폴더에 js 파일을 만듭니다. ) 폴더. 폴더 구조는 다음과 같습니다.


src/css/app.css 파일을 열고 거기에 tailwind 지시문을 배치하십시오.

@tailwind base;
@tailwind components;
@tailwind utilities;

src/js/app.js 파일을 열고 해당 파일에 jquery를 설정합니다.

window.$ = window.jQuery = require('jquery');


이제 우리 프로젝트에서 Daisy UI 및 twailwind의 타이포그래피 도우미를 구성할 시간입니다. tailwind.config.js 파일을 열고 플러그인 배열에 이 두 항목을 추가합니다.

{
  ...,
  plugins: [
    require('@tailwindcss/typography'),
    require('daisyui')
  ]
}


그 후 tailwind.config.js는 다음과 같이 보일 것입니다. 또한 콘텐츠에 템플릿과 js 파일을 추가하여 tailwindcss가 프로젝트에서 사용 중인 클래스를 생성하고 프로젝트에서 사용하지 않는 클래스를 빌드 파일에서 제거하여 CSS 파일을 최소화합니다.

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
      './application/views/**/*.jinja2',
      './application/assets/js/**/*.js',
  ],
  theme: {
    extend: {},
  },
  plugins: [
      require('@tailwindcss/typography'),
      require('daisyui')
  ],
}


이제 빌드 도구Laravel Mix를 사용하여 이러한 자산을 빌드할 시간입니다. 루트 디렉토리에 webpack.mix.js 파일을 만들고 이 모든 구성을 해당 파일에 배치합니다.

const mix = require('laravel-mix');


mix.options({
  fileLoaderDirs: {
    fonts: "assets/fonts",
    images: "assets/images",
  },
});



mix.js("src/js/app.js", "application/assets/js")
  .postCss("src/css/app.css", "application/assets/css", [
    require("tailwindcss"),
  ]);


개발 및 프로덕션용 자산을 빌드하려면 스크립트가 필요하므로 해당 행을 추가한 후 package.json 파일에 해당 행을 추가하십시오. package.json은 다음과 같습니다.

{
  "name": "flask_blog",
  "version": "1.0.0",
  "repository": "[email protected]:MuhammadSaim/flask_tutorial_blog.git",
  "author": "Muhammad Saim",
  "license": "MIT",
  "scripts": {
    "dev": "npm run development",
    "development": "mix",
    "watch": "mix watch",
    "watch-poll": "mix watch -- --watch-options-poll=1000",
    "hot": "mix watch --hot",
    "prod": "npm run production",
    "production": "mix --production"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.8",
    "laravel-mix": "^6.0.49",
    "postcss": "^8.4.16",
    "tailwindcss": "^3.1.8"
  },
  "dependencies": {
    "@tailwindcss/typography": "^0.5.4",
    "daisyui": "^2.22.0",
    "jquery": "^3.6.0"
  }
}


이러한 구성을 추가한 후 명령을 실행하여 이러한 자산을 빌드할 수 있습니다.

개발

yarn dev


생산

yarn prod


감시 서버

yarn watch


이것들은 자산을 assetsapplication/assets 디렉토리에 빌드할 것입니다. 또한 .gitignore 디렉토리의 application 파일에서 이 디렉토리를 무시할 수도 있습니다.



이제 views 3개의 폴더 includes , layoutspages를 만들고 app.jinja2 이름으로 레이아웃에 하나의 파일을 만들고 pages 디렉토리에 두 번째 파일을 home.jinja2 이름으로 만듭니다. 두 개의 파일이 여기에 있습니다.

app.jinja2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/app.css') }}">
    <title>{% block title %}{% endblock %}</title>
</head>
<body class="prose">

    {% block content %}{% endblock %}

    <script src="{{ url_for('static', filename='css/app.css') }}"></script>
</body>
</html>


home.jinja2

{% extends 'layouts/app.jinja2' %}


{% block title %} Home {% endblock %}


{% block content %}
    <h1 class="text-3xl">Home</h1>
    <button class="btn btn-primary">Home</button>
{% endblock %}


모든 템플릿을 설정한 후 컨트롤러에서 약간 변경할 수 있습니다home.py.

from flask import Blueprint, render_template

controller = Blueprint('home', __name__)


@controller.route('/', methods=['GET'])
def index():
    return render_template('pages/home.jinja2')


이제 응용 프로그램을 실행하십시오.

python run.py


URL 열기https://127.0.0.1:5000


GitHub Repo에서 업데이트된 코드를 받을 수 있습니다.

함께해주셔서 감사합니다.

이 게시물을 진행하면서 문제가 발생하면 언제든지 댓글을 남겨주세요.