Professional Documents
Culture Documents
Release 0.2.0
Grey Li
1 Contents 3
1.1 Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Advanced Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5 Try Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 API Reference 13
2.1 API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Changelog 15
3.1 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4 Development 19
5 Authors 21
6 License 23
i
ii
Flask-CKEditor Documentation, Release 0.2.0
CKEditor integration for Flask, add support to image upload, code syntax highlight and more.
Contents 1
Flask-CKEditor Documentation, Release 0.2.0
2 Contents
CHAPTER 1
Contents
1.1.1 Installation
1.1.2 Initialization
This extension needs to be initialized in the usual way before it can be used:
app = Flask(__name__)
ckeditor = CKEditor(app)
This extension also supports the Flask application factory pattern by allowing you to create a CKEditor object and
then separately initialize it for an app:
ckeditor = CKEditor()
def create_app():
app = Flask(__name__)
...
ckeditor.init_app(app)
...
return app
3
Flask-CKEditor Documentation, Release 0.2.0
In the template which you want to put a CKEditor textarea, call ckeditor.load() in <head></head> or before
</body>:
<body>
...
{{ ckeditor.load() }}
</body>
In default, it will load the CKEditor resources from CND (cdn.ckeditor.com), you can set
CKEDITOR_SERVE_LOCAL to True to use built-in resources. You can use custom_url to load your cus-
tom CKEditor build:
{{ ckeditor.load(custom_url=url_for('static', filename='ckeditor/ckeditor.js')) }}
CKEditor provide three type of preset (i.e. basic, standard and full), this method default to load standard.
You can use pkg_type parameter or CKEDITOR_PKG_TYPE configuration variable to set the package type. For
example:
{{ ckeditor.load(pkg_type="basic") }}
Or:
app = Flask(__name__)
app.config['CKEDITOR_PKG_TYPE'] = 'basic'
ckeditor = CKEditor(app)
This method is just a helper to generate <script> to include CKEditor resources, you can also write <script>
element directly:
<script src="https://cdn.ckeditor.com/4.10.0/standard/ckeditor.js"></script>
<form method="post">
{{ ckeditor.create() }}
<input type="submit">
</form>
Since the CKEditor textarea is just a normal <textarea> element, so you can get the data from request.form
by passing ckeditor as key:
@app.route('/write')
def new_post():
if request.method == 'POST':
data = request.form.get('ckeditor') # <--
(continues on next page)
4 Chapter 1. Contents
Flask-CKEditor Documentation, Release 0.2.0
return render_template('index.html')
When using Flask-WTF/WTForms, you can import the CKEditorField provided by Flask-CKEditor and use it
just like StringField:
class PostForm(FlaskForm):
title = StringField('Title')
body = CKEditorField('Body') # <--
submit = SubmitField('Submit')
One more step is to call ckeditor.config() and pass the CKEditorField attribute’s name:
<form method="post">
{{ form.title() }}
{{ form.body() }}
{{ form.submit() }}
</form>
{{ ckeditor.load() }}
{{ ckeditor.config(name='body') }}
</body>
In the view function, you can get the data either by request.form.get('body') or form.body.data.
1.2 Configuration
Except CKEDITOR_SERVE_LOCAL and CKEDITOR_PKG_TYPE, when you use other configuration variable, you
have to call ckeditor.config() in template to make them register with CKEditor:
<body>
... <!-- {{ ckeditor.load() }} or <script src="/path/to/ckeditor.js"> -->
{{ ckeditor.config() }}
</body>
Note: When using Flask-WTF/WTForms, you have to pass the field name as name in ckeditor.config(),
for example ckeditor.config(name='description'). If you create the CKEditor through ckeditor.
create(), the default value (ckeditor) will be used.
1.2. Configuration 5
Flask-CKEditor Documentation, Release 0.2.0
{{ ckeditor.config(custom_config="uiColor: '#9AB8F3'") }}
Keep it mind that the proper syntax for each option is configuration name : configuration value.
You can use comma to separate multiple key-value pairs. See the list of available configuration settings on CKEditor
documentation.
If you need create multiple text area in one page, here are some tips:
Without Flask-WTF/WTForms
Create two text area with different name and configure it with the name:
6 Chapter 1. Contents
Flask-CKEditor Documentation, Release 0.2.0
<h1>About me</h1>
{{ ckeditor.create(name='bio') }}
<h1>About my team</h1>
{{ ckeditor.create(name='team') }}
{{ ckeditor.load() }}
{{ ckeditor.config(name='bio') }}
{{ ckeditor.config(name='team') }}
With Flask-WTF/WTForms
When create multiple form with Flask-WTF/WTForms, you just need to create multiple CKEditorField field:
class PostForm(FlaskForm):
title = StringField('Title')
bio = CKEditorField('About me') # <--
team = CKEditorField('About my team') # <--
submit = SubmitField('Submit')
In the template, you render them and configure them with the right name:
{{ form.bio() }}
{{ form.team() }}
{{ form.submit() }}
{{ ckeditor.load() }}
{{ ckeditor.config(name='bio') }}
{{ ckeditor.config(name='team') }}
Sometimes you may want to use different configuration for multiple text area, in this case, you can pass the specific
keyword arguments into ckeditor.config() directly.
The keyword arguments should mapping the corresponding configration variable in this way:
• CKEDITOR_LANGUAGE –> language
• CKEDITOR_WIDTH –> width
• CKEDITOR_FILE_UPLOADER –> file_uploader
• etc
example:
{{ ckeditor.config(lanuage='en', width=500) }}
1.2. Configuration 7
Flask-CKEditor Documentation, Release 0.2.0
In the end, the keyword argument you pass will overwrite the corresponding configurations.
Comparatively, you can use serve_local and pkg_type in ckeditor.load() to overwrite
CKEDITOR_SERVE_LOCAL and CKEDITOR_PKG_TYPE.
The bulit-in CKEditor package include a File Browser plugin. With this plugin, you can upload and insert image with
image widget. You need set CKEDITOR_FILE_UPLOADER to the URL or endpoint which handle upload files, and
the upload view must return upload_success() call with the uploaded image’s url. Usually, you also need to
validate uploaded image, then you can use upload_fail() to return an error message with message argument.
If message was None, the value in configuration variable CKEDITOR_UPLOAD_ERROR_MESSAGE will be used,
default to Upload failed.. Here is the full example:
@app.route('/files/<path:filename>')
def uploaded_files(filename):
path = '/the/uploaded/directory'
return send_from_directory(path, filename)
@app.route('/upload', methods=['POST'])
def upload():
f = request.files.get('upload')
# Add more validations here
extension = f.filename.split('.')[1].lower()
if extension not in ['jpg', 'gif', 'png', 'jpeg']:
return upload_fail(message='Image only!')
f.save(os.path.join('/the/uploaded/directory', f.filename))
url = url_for('uploaded_files', filename=f.filename)
return upload_success(url=url) # return upload_success call
it’s defined by CKEditor and it’s not the name of the view function.
In the template, you have to call ckeditor.config() to make configuration work:
{{ ckeditor.config() }}
Tip: When using Flask-WTF/WTForms, you have to pass the field name as name in ckeditor.config(),
for example ckeditor.config(name='description'). If you create the CKEditor through ckeditor.
create(), the default value (ckeditor) will be used.
Now you will find the Upload tab appear in image widget. Besides, you can drag and drop image directly into the
editor area or copy and paste the image (CKEditor >= 4.5).
8 Chapter 1. Contents
Flask-CKEditor Documentation, Release 0.2.0
If the CKEditor version you use was under 4.5, you will need to use @ckeditor.uploader to decorated the view
function that handle the file upload. The upload view must return the uploaded image’s url. For example:
@app.route('/files/<filename>')
def uploaded_files(filename):
path = '/the/uploaded/directory'
return send_from_directory(path, filename)
@app.route('/upload', methods=['POST'])
@ckeditor.uploader
def upload():
f = request.files.get('upload')
f.save(os.path.join('/the/uploaded/directory', f.filename))
url = url_for('uploaded_files', filename=f.filename)
return url
You can use configuration variable CKEDITOR_UPLOAD_ERROR_MESSAGE to customize the error message when
upload failed, default to Upload failed.
it’s defined by CKEditor and it’s not the name of the view function.
In the template, you have to call ckeditor.config() to make configuration work:
{{ ckeditor.config() }}
Tip: When using Flask-WTF/WTForms, you have to pass the field name as name in ckeditor.config(),
for example ckeditor.config(name='description'). If you create the CKEditor through ckeditor.
create(), the default value (ckeditor) will be used.
Now you will find the Upload tab appear in image widget.
app = Flask(__name__)
csrf = CSRFProtect(app)
Make sure to set the secret key and set CKEDITOR_ENABLE_CSRF to True. Now all the image upload request will
be protected!
The bulit-in CKEditor package include a Code Snippet plugin. You need to set
CKEDITOR_ENABLE_CODESNIPPET to True to enable it. You can set the code theme through configura-
tion option CKEDITOR_CODE_THEME. The default theme was monokai_sublime. See all available themes and
the list of valid theme string on this page.
Another step was load code theme resources in the page you want to display the text:
<head>
...
{{ ckeditor.load_code_theme() }}
</head>
1.4 FAQ
10 Chapter 1. Contents
Flask-CKEditor Documentation, Release 0.2.0
12 Chapter 1. Contents
CHAPTER 2
API Reference
If you are looking for information on a specific function, class or method, this part of the documentation is for you.
2.1.4 Utils
13
Flask-CKEditor Documentation, Release 0.2.0
Changelog
3.1 Changelog
3.1.1 0.4.3
3.1.2 0.4.2
3.1.3 0.4.1
3.1.4 0.4.0
15
Flask-CKEditor Documentation, Release 0.2.0
3.1.5 0.3.3
3.1.6 0.3.2
3.1.7 0.3.1
3.1.8 0.3
3.1.9 0.2
16 Chapter 3. Changelog
Flask-CKEditor Documentation, Release 0.2.0
3.1.10 0.1
Initialize release.
3.1. Changelog 17
Flask-CKEditor Documentation, Release 0.2.0
18 Chapter 3. Changelog
CHAPTER 4
Development
We welcome all kinds of contributions. You can run test like this:
19
Flask-CKEditor Documentation, Release 0.2.0
20 Chapter 4. Development
CHAPTER 5
Authors
Maintainer: Grey Li
See also the list of contributors who participated in this project.
21
Flask-CKEditor Documentation, Release 0.2.0
22 Chapter 5. Authors
CHAPTER 6
License
This project is licensed under the MIT License (see the LICENSE file for details).
23
Flask-CKEditor Documentation, Release 0.2.0
24 Chapter 6. License
Python Module Index
f
flask_ckeditor, 13
flask_ckeditor.utils, 13
25
Flask-CKEditor Documentation, Release 0.2.0
F
flask_ckeditor (module), 13
flask_ckeditor.utils (module), 13
27