Usage¶
Media assets management for Invenio.
Invenio-Assets helps you with integration of webassets, installation of NPM packages, and process of collecting static files.
Initialization¶
First create a Flask application:
>>> from flask import Flask
>>> app = Flask('myapp')
Next, initialize your extension:
>>> from invenio_assets import InvenioAssets
>>> assets = InvenioAssets(app)
During initialization two Flask extensions
flask_webpackext.ext.FlaskWebpackExt
and
flask_collect.Collect
are instantiated and configured.
Bundles specified in the entry point groups called invenio_assets.webpack
are automatically registered by Invenio-Assets.
Using Flask-WebpackExt¶
Bundles¶
The Flask-WebpackExt
package provides a class
flask_webpackext.bundle.WebpackBundle
for declaring the needed
assets and NPM dependencies of each one of your modules.
from flask_webpackext import WebpackBundle
bundle1 = WebpackBundle(
__name__,
'./modules/module1/static',
entry={
'module1-app': './js/module1-app.js',
},
dependencies={
'jquery': '^3.2.1'
}
)
The NPM dependencies defined in the bundles will be used to generate the
package.json
file.
Entry points loading¶
Invenio-Assets will automatically load bundles defined by the entry point
group invenio_assets.webpack
. Example:
# setup.py
setup(
# ...
entry_points={
'invenio_assets.webpack': [
'mybundle = mypackage.bundles:mybundle',
],
# ...
}
# ...
)
Command-line interface¶
We can now build the assets:
$ flask webpack buildall
The command will copy all files from the src folder to the application instance folder designated for the Webpack project, download the npm packages and run Webpack to build our assets.
Alternatively, we can execute each build step separately with the following flask webpack commands:
create
- Copy all sources to the working directory.install
- Run npm install command and download all dependencies.build
- Run npm run build.
Additionally if we have some static files we should collect them:
$ flask collect -v