Skip to main content

Flask-CodeMirror

version license

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.

Metadata

Release files for flask-codemirror 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-codemirror 1.3
File Size Uploaded
flask-codemirror-1.3.tar.gz 36.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-codemirror 1.3
File Interpreter ABI Platform
flask_codemirror-1.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 55.9 kB

Release files / flask-codemirror-1.3.tar.gz

Download URL flask-codemirror-1.3.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8697f37aa63d9fb0252956357ef46814abd0c2b78e8fe232ce4414ec12bf3342
BLAKE2b-256 checksum
How to use checksums
20caa4ec76adf51e063cc9d84dc7a820141021855e728bffce111e07f62bc02e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / flask_codemirror-1.3-py2.py3-none-any.whl

Download URL flask_codemirror-1.3-py2.py3-none-any.whl
Size 19.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
66a99cac60b799f588eb9fb2730b1463ce6bb993ead8f31f55df5c40339a9f34
BLAKE2b-256 checksum
How to use checksums
49bede1c6641f8a9d8b4c400dc56217f22ebdd7645d77defcb337c69c85b0781
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files

1.2

3 release files

1.1

2 release files

1.0

2 release files

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page