Flask-Assets

This week, I had a task to implement the SCSS with the Python Flask application. The project uses Jinja as a template engine and Flask for web Framework. This is the first time I have used SCSS, so I would love to take some introductory notes.


This content originally appeared on DEV Community and was authored by Emily

This week, I had a task to implement the SCSS with the Python Flask application. The project uses Jinja as a template engine and Flask for web Framework. This is the first time I have used SCSS, so I would love to take some introductory notes.

Why SCSS

We all know that CSS is Cascading style language using to style webpage. SCSS stands for Sassy CSS, which is CSS pre-processor that give developer ability to use variables, nested syntax, imports, etc. In the pre-processing stage, SCSS files will be converted to CSS to be ready for the browser.
The biggest advantages of SCSS over CSS is removing code redundant and utilizing code reuse. For example:
CSS Code

.container {
  background-color: white;
  padding: 20px;
}
.container h1{
  color: red;
  text-align: center;
  margin-top: 20px;
}
.container p{
  color: red;
  text-align: center;
  margin-top: 10px;
}

SCSS Code

$color-container: white;
$color-text: red;
.container {
  background-color: white;
  padding: 20px;
  h1 {     // Notice here we don't need to repeat .container class
  color: $color-text;
  text-align: center;
  margin-top: 20px; 
  }
  p {
  color: $color-text;
  text-align: center;
  margin-top: 10px;
  }
}

Install Flask-assets and pyscss

To compile SCSS to CSS, I need Flask-Assets to manage the assets/static files of my Flask application and Pyscss as CSS compiler to convert SCSS markup to real CSS.

pip install Flask-Assets
pip install pyscss

My application structure:

Folder    
├───App
│   ├───Config
│   │   └───__pycache__
│   ├───Constants
│   │   └───__pycache__
│   ├───flask_session
│   ├───Models
│   │   └───__pycache__
│   ├───static 
│   │   ├───Gen
│   │   ├───SCSS
│   ├───templates
│   │   ├───example.html
|   ├───__init__.py
├───Setup
│   ├───PIP
│   └───QRIOUS
├───application.py

According to the documentation, Environment object is used to hold a collection of bundles and configuration.
A bundle object contains source files, filters _ pyscss in my case, and the output file. It is important to note that all paths in the bundle are relative to my flask application's static folder. For example: output='Gen/example.css' , the output CSS file will be in static\Gen folder.
My code:

from flask import Flask
from flask_assets import Environment, Bundle
app = Flask(__name__)
assets = Environment(app) # create an Environment instance
bundles = {  # define nested Bundle
  'example_style': Bundle(
            'SCSS/example.scss',
            filters='pyscss',
            output='Gen/example.css',
  )
} 
assets.register(bundles) # register name for every bundle in bundles object

And lately, I can insert my bundle by using its name in the block assets of jinja template. Flask-Assets will help merge and compress my bundle source files the first time when the template is rendered. Moreover, it also watches the change of the source files and will update the compressed file when necessary.

<head>
    {% assets "example_style" %}
    <link rel="stylesheet" type="text/css" href="{{ ASSET_URL }}">
    {% endassets %}
</head>

I found that Flask-assets is easy to use, and it becomes convenient for pre-processing files such as SCSS files or JS files.


This content originally appeared on DEV Community and was authored by Emily


Print Share Comment Cite Upload Translate Updates
APA

Emily | Sciencx (2021-12-21T21:54:21+00:00) Flask-Assets. Retrieved from https://www.scien.cx/2021/12/21/flask-assets/

MLA
" » Flask-Assets." Emily | Sciencx - Tuesday December 21, 2021, https://www.scien.cx/2021/12/21/flask-assets/
HARVARD
Emily | Sciencx Tuesday December 21, 2021 » Flask-Assets., viewed ,<https://www.scien.cx/2021/12/21/flask-assets/>
VANCOUVER
Emily | Sciencx - » Flask-Assets. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/21/flask-assets/
CHICAGO
" » Flask-Assets." Emily | Sciencx - Accessed . https://www.scien.cx/2021/12/21/flask-assets/
IEEE
" » Flask-Assets." Emily | Sciencx [Online]. Available: https://www.scien.cx/2021/12/21/flask-assets/. [Accessed: ]
rf:citation
» Flask-Assets | Emily | Sciencx | https://www.scien.cx/2021/12/21/flask-assets/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.