Use CodeMirror Javascript library with Flask-WTF
Project description
Implementation of source code editor for Flask and Flask-WTF using CodeMirror Javascript library
Installation
$ pip install flask-codemirror
Example
A simple example of how to use this module
from flask_wtf import FlaskForm from flask_codemirror.fields import CodeMirrorField from wtforms.fields import SubmitField class MyForm(FlaskForm): source_code = CodeMirrorField(language='python', config={'lineNumbers': 'true'}) submit = SubmitField('Submit')
The CodeMirrorField works exactly like a TextAreaField
@app.route('/', methods = ['GET', 'POST']) def index(): form = MyForm() if form.validate_on_submit(): text = form.source_code.data return render_template('index.html', form=form)
The module needs to be initialized in the usual way and can be configured using app.config keys
from flask import Flask from flask_codemirror import CodeMirror # mandatory CODEMIRROR_LANGUAGES = ['python', 'yaml', 'htmlembedded'] WTF_CSRF_ENABLED = True SECRET_KEY = 'secret' # optional CODEMIRROR_THEME = '3024-day' CODEMIRROR_ADDONS = ( ('ADDON_DIR','ADDON_NAME'), ) app = Flask(__name__) app.config.from_object(__name__) codemirror = CodeMirror(app)
The config CODEMIRROR_LANGUAGES needs to be initialized to load JavaScript. It defined all the languages you want to edit with your fields. The config CODEMIRROR_THEME is optional and is used to style your TextArea using css from CodeMirror website. The config CODEMIRROR_ADDONS is optional and can enable many cool options see Codemirror Addons for available addons.
Finally, the template needs the support JavaScript code added, by calling codemirror.include_codemirror() :
<html> <head> {{ codemirror.include_codemirror() }} </head> <body> <form method="POST"> {{ form.csrf_token }} {{ form.source_code }} <input type="submit" value="OK"> </form> </body> </html>
The Javascript classes are imported from a CDN, there are no static files that need to be served by the application.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-codemirror-1.3.tar.gz
.
File metadata
- Download URL: flask-codemirror-1.3.tar.gz
- Upload date:
- Size: 36.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/30.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/23.2.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8697f37aa63d9fb0252956357ef46814abd0c2b78e8fe232ce4414ec12bf3342 |
|
MD5 | fee181f19a969ff63d16b6d1b1c2867d |
|
BLAKE2b-256 | 20caa4ec76adf51e063cc9d84dc7a820141021855e728bffce111e07f62bc02e |
File details
Details for the file flask_codemirror-1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: flask_codemirror-1.3-py2.py3-none-any.whl
- Upload date:
- Size: 19.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/30.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/23.2.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66a99cac60b799f588eb9fb2730b1463ce6bb993ead8f31f55df5c40339a9f34 |
|
MD5 | 51a8911bca2234df3d74c67e482694e2 |
|
BLAKE2b-256 | 49bede1c6641f8a9d8b4c400dc56217f22ebdd7645d77defcb337c69c85b0781 |