Registry / web-framework / flask-moment

flask-moment

JSON →
library1.0.6pypypi✓ verified 87d ago

Flask-Moment is an extension for the Flask web application framework that simplifies the formatting and rendering of dates and times in Jinja2 templates using the client-side JavaScript library moment.js. As of its latest release, 1.0.6, the library remains active, with the most recent version released on May 28, 2024. It typically sees a few releases per year, primarily for maintenance and minor enhancements.

pip install flask-moment
INSTALL
IMPORT
SIG · FLASK-MOMENT
F
flask-moment
web-frameworkpythonv1.0.6
Install
2.3s avg
Import
478ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.496s · 23.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.3s · import 0.460s · 24MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Moment
from flask_moment import Moment
from flask.ext.moment import Moment
The `flask.ext.` prefix was deprecated in Flask 0.11 and removed in Flask 1.0. All modern Flask extensions are imported directly from their package name.

Initialize the `Moment` extension with your Flask application. In your Jinja2 template, include `{{ moment.include_moment() }}` in the `<head>` section to load the necessary JavaScript libraries. Then, you can use the `moment()` function in your templates to format `datetime` objects or render current time, utilizing various moment.js formatting options.

from flask import Flask, render_template from flask_moment import Moment from datetime import datetime app = Flask(__name__) app.config['SECRET_KEY'] = 'a-very-secret-key' moment = Moment(app) @app.route('/') def index(): return render_template('index.html', current_time=datetime.utcnow()) # --- In templates/index.html --- # <!DOCTYPE html> # <html lang="en"> # <head> # <meta charset="UTF-8"> # <title>Flask-Moment Example</title> # {{ moment.include_moment() }} # </head> # <body> # <p>The current time is: {{ moment(current_time).format('MMMM Do YYYY, h:mm:ss a') }}.</p> # <p>A relative time: {{ moment(current_time).fromNow() }}.</p> # </body> # </html>
Debug
Known issues
breakingStarting with Flask-Moment 1.0.0, the `moment.include_jquery()` function was removed. jQuery is no longer a dependency of `flask-moment` itself.
fix
Remove `{{ moment.include_jquery() }}` from your templates. Only `{{ moment.include_moment() }}` is required. If your application still requires jQuery, you must include it separately.
affects: >=1.0.0
gotchaThe `refresh=True` argument for rendering functions like `format()` will only refresh the *client-side* display, not re-evaluate the Python `datetime` object passed from the server. If you pass a fixed `datetime` object, `refresh=True` will not make it update like a live clock, but only update relative formats (e.g., 'X seconds ago').
fix
Use `refresh=True` only with dynamic rendering functions like `fromNow()` or `fromTime()`. For a live-updating clock or similar, consider client-side JavaScript solutions or periodic AJAX calls to fetch updated `datetime` objects from the server.
affects: All versions
gotchaFlask-Moment generates inline JavaScript for its functionality, which can be blocked by strict Content Security Policies (CSP). This may result in 'Refused to execute inline script' errors.
fix
To comply with CSP, you can use `moment.include_moment(no_js=True)` and manually load `moment.js` (and optionally `moment-timezone.js`) from a trusted CDN or local source with appropriate SRI hashes. Then, you'll need to wrap the output of `moment.flask_moment_js()` in a script tag with a nonce. Refer to the `flask-moment` documentation for advanced CSP integration.
affects: All versions
deprecatedThe underlying moment.js library, which Flask-Moment relies on, is now considered a legacy project in maintenance mode. Its developers recommend that new projects avoid it and choose modern alternatives due to bundle size, mutability issues, and a different API design philosophy.
fix
For new projects, consider using a different Flask extension for date/time handling that integrates with a modern JavaScript date library (e.g., `date-fns`, `Luxon`). For existing projects, be aware of `moment.js`'s maintenance status and potential future limitations.
affects: All versions (due to upstream library status)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flask_moment'
The `flask-moment` package is not installed or the Python environment where the application is running does not have it accessible. This can also occur if `flask_moment` is misspelled (e.g., `flask_Moment`).
fix
Ensure the package is correctly installed: `pip install flask-moment`. If using a virtual environment, ensure it is activated. Check for correct casing in the import statement: `from flask_moment import Moment`.
ImportError: No module named 'flask.ext.moment'
Using an outdated import path for Flask extensions. The `flask.ext.` prefix was removed in Flask 1.0.
fix
Update your import statement to the modern form: `from flask_moment import Moment`.
Flask-Moment not displaying anything on template (or showing raw `datetime` objects)
The JavaScript assets required by Flask-Moment are not being loaded in the browser. This usually happens if `{{ moment.include_moment() }}` is missing from the template or placed incorrectly (e.g., after the `<body>` tag or within a separate block that isn't rendered).
fix
Ensure `{{ moment.include_moment() }}` is present within the `<head>` section of your base template (or any template using Flask-Moment) to ensure the client-side JavaScript is initialized.
Moment.js CDN returning 404 errors in the browser console
Older versions of Flask-Moment might reference specific `moment.js` versions from CDNs that are no longer available or have changed their asset paths. This was a known issue with Flask-Moment 0.10 trying to load moment.js 2.26.0.
fix
Upgrade to the latest version of `flask-moment` (`pip install --upgrade flask-moment`). The `include_moment()` function should automatically reference a current and stable version of `moment.js`. If issues persist, consider using the `version` argument in `include_moment()` to specify a known working version, or provide a `local_js` path to a self-hosted `moment.js` file.
Upgrade
Version history
1.0.6latest on PyPI · released May 28, 2024
Audit
Dependencies
FlaskrequiredCore dependency as Flask-Moment is a Flask extension.
Agent activity
2 hits · last 30 days
node
2
Resources
flask-moment — pip install flask-moment · libregistry