flask 几个扩展应用

Posted on Posted in flask

flask 扩展

flask 扩展搜索网址 Flask Toolbox

flask-script

代码

───────┬──────────────────
       │ File: index.py
───────┼──────────────────
   1   │ #!/usr/bin/env python3
   2   │ # -*- coding: utf-8 -*-
   3   │ # vim:fenc=utf-8
   4   │ # Create Time: 2018-12-06 09:49:05
   5   │ # Last Modified: 2018-12-06 09:54:33
   6   │ from flask import Flask
   7   │ from flask_script import Manager
   8   │ app = Flask(__name__)
   9   │ manager = Manager(app)
  10   │
  11   │
  12   │ @app.route('/')
  13   │ def index():
  14   │     return '<h1>hello world</h1>'
  15   │
  16   │
  17   │ if __name__ == '__main__':
  18   │     manager.run()

使用

python index.py
python index.py runserver --help
python index.py runserver --host 0.0.0.0

flask-bootstrap

index.py

───────┬─────────────────────
       │ File: index.py
───────┼─────────────────────
   1   │ #!/usr/bin/env python3
   2   │ # -*- coding: utf-8 -*-
   3   │ # vim:fenc=utf-8
   4   │ # Create Time: 2018-12-06 09:49:05
   5   │ # Last Modified: 2018-12-06 10:03:07
   6   │ from flask import Flask, render_template
   7   │ from flask_bootstrap import Bootstrap
   8   │ app = Flask(__name__)
   9   │ bootstrap = Bootstrap(app)
  10   │
  11   │
  12   │ @app.route('/')
  13   │ def index():
  14   │     return '<h1>hello world</h1>'
  15   │
  16   │
  17   │ @app.route('/user')
  18   │ def user():
  19   │     return render_template('user.html', name='user01')
  20   │
  21   │
  22   │ if __name__ == '__main__':
  23   │     app.run(debug=True)
───────┴─────────────────────

templates/user.html

───────┬────────────────────
       │ File: templates/user.html
───────┼────────────────────
   1   │ {% extends "bootstrap/base.html" %}
   2   │ {% block title %}Flasky{% endblock %}
   3   │ {% block navbar %}
   4   │ <div class="navbar navbar-inverse" role="navigation">
   5   │     <div class="container">
   6   │         <div class="navbar-header">
   7   │             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
   8   │                 <span class="sr-only">Toggle navigation</span>
   9   │                 <span class="icon-bar"></span>
  10   │                 <span class="icon-bar"></span>
  11   │                 <span class="icon-bar"></span>
  12   │             </button>
  13   │             <a class="navbar-brand" href="/">Flasky</a>
  14   │         </div>
  15   │         <div class="navbar-collapse collapse">
  16   │             <ul class="nav navbar-nav">
  17   │                 <li><a href="/">Home</a></li>
  18   │             </ul>
  19   │         </div>
  20   │     </div>
  21   │ </div>
  22   │ {% endblock %}
  23   │
  24   │ {% block content %}
  25   │ <div class="container">
  26   │     <div class="page-header">
  27   │         <h1>Hello, {{ name }}!</h1>
  28   │     </div>
  29   │ </div>
  30   │ {% endblock %}
  31   │
  32   │ {% block scripts %}
  33   │ {{ super() }}
  34   │ <script>
  35   │     console.log('test-content');
  36   │ </script>
  37   │ {% endblock %}
───────┴────────────────────────────────

flask-moment

index.py

───────┬────────────────────
       │ File: index.py
───────┼────────────────────
   1   │ #!/usr/bin/env python3
   2   │ # -*- coding: utf-8 -*-
   3   │ # vim:fenc=utf-8
   4   │ # Create Time: 2018-12-06 09:49:05
   5   │ # Last Modified: 2018-12-06 10:21:22
   6   │ from flask import Flask, render_template
   7   │ from flask_bootstrap import Bootstrap
   8   │ from datetime import datetime
   9   │ from flask_moment import Moment
  10   │ app = Flask(__name__)
  11   │ moment = Moment(app)
  12   │ bootstrap = Bootstrap(app)
  13   │
  14   │
  15   │ @app.route('/')
  16   │ def index():
  17   │     return render_template('index.html',
  18   │                            name='user01',
  19   │                            current_time=datetime.utcnow())
  20   │
  21   │
  22   │ if __name__ == '__main__':
  23   │     app.run(debug=True)
───────┴───────────────────

templates/index.html

───────┬────────────────────
       │ File: templates/index.html
───────┼────────────────────
   1   │ {% extends "bootstrap/base.html" %}
   2   │ {% block title %}Flasky{% endblock %}
   3   │ {% block navbar %}
   4   │ <div class="navbar navbar-inverse" role="navigation">
   5   │     <div class="container">
   6   │         <div class="navbar-header">
   7   │             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
   8   │                 <span class="sr-only">Toggle navigation</span>
   9   │                 <span class="icon-bar"></span>
  10   │                 <span class="icon-bar"></span>
  11   │                 <span class="icon-bar"></span>
  12   │             </button>
  13   │             <a class="navbar-brand" href="/">Flasky</a>
  14   │         </div>
  15   │         <div class="navbar-collapse collapse">
  16   │             <ul class="nav navbar-nav">
  17   │                 <li><a href="/">Home</a></li>
  18   │             </ul>
  19   │         </div>
  20   │     </div>
  21   │ </div>
  22   │ {% endblock %}
  23   │
  24   │ {% block content %}
  25   │ <div class="container">
  26   │     <div class="page-header">
  27   │         <h1>Hello, {{ name }}!</h1>
  28   │         <p>The local date and time is {{ moment(current_time).format('LLL') }}.</p><p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
  29   │     </div>
  30   │ </div>
  31   │ {% endblock %}
  32   │
  33   │ {% block scripts %}
  34   │ {{ super() }}
  35   │ <script>
  36   │     console.log('test-content');
  37   │ </script>
  38   │ {{ moment.include_moment() }}
  39   │ {{ moment.lang('zh-CN')}}
  40   │ {% endblock %}
───────┴─────────────────────

flask-wtf

index.py

───────┬────────────────────
       │ File: index.py
───────┼────────────────────
   1   │ #!/usr/bin/env python3
   2   │ # -*- coding: utf-8 -*-
   3   │ # vim:fenc=utf-8
   4   │ # Create Time: 2018-12-06 09:49:05
   5   │ # Last Modified: 2018-12-06 10:52:06
   6   │ from flask import Flask, render_template
   7   │ from flask_bootstrap import Bootstrap
   8   │ from datetime import datetime
   9   │ from flask_moment import Moment
  10   │ from flask_wtf import FlaskForm
  11   │ from wtforms import StringField, SubmitField
  12   │ from wtforms.validators import Required
  13   │ app = Flask(__name__)
  14   │ app.config['SECRET_KEY'] = 'yigehennanshibiedemimachuan'
  15   │ moment = Moment(app)
  16   │ bootstrap = Bootstrap(app)
  17   │
  18   │
  19   │ @app.route('/')
  20   │ def index():
  21   │     form = NameForm()
  22   │     name = None
  23   │     if form.validate_on_submit():
  24   │         name = form.name.data
  25   │         form.name.data = ''
  26   │     return render_template('index.html',
  27   │                            name=name,
  28   │                            current_time=datetime.utcnow(),
  29   │                            form=form)
  30   │
  31   │
  32   │ class NameForm(FlaskForm):
  33   │     name = StringField('What is your name?', validators=[Required])
  34   │     submit = SubmitField('Submit')
  35   │
  36   │
  37   │ if __name__ == '__main__':
  38   │     app.run(debug=True)
───────┴─────────────────────

templates/index.html

───────┬────────────────────
       │ File: templates/index.html
───────┼────────────────────
   1   │ {% extends "bootstrap/base.html" %}
   2   │ {% block title %}Flasky{% endblock %}
   3   │ {% block navbar %}
   4   │ <div class="navbar navbar-inverse" role="navigation">
   5   │     <div class="container">
   6   │         <div class="navbar-header">
   7   │             <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
   8   │                 <span class="sr-only">Toggle navigation</span>
   9   │                 <span class="icon-bar"></span>
  10   │                 <span class="icon-bar"></span>
  11   │                 <span class="icon-bar"></span>
  12   │             </button>
  13   │             <a class="navbar-brand" href="/">Flasky</a>
  14   │         </div>
  15   │         <div class="navbar-collapse collapse">
  16   │             <ul class="nav navbar-nav">
  17   │                 <li><a href="/">Home</a></li>
  18   │             </ul>
  19   │         </div>
  20   │     </div>
  21   │ </div>
  22   │ {% endblock %}
  23   │
  24   │ {% block content %}
  25   │ <div class="container">
  26   │     <div class="page-header">
  27   │         <h1>Hello, {{ name }}!</h1>
  28   │         <p>The local date and time is {{ moment(current_time).format('LLL') }}.</p><p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
  29   │         {% import "bootstrap/wtf.html" as wtf %}
  30   │         {{ wtf.quick_form(form) }}
  31   │         </form>
  32   │     </div>
  33   │ </div>
  34   │ {% endblock %}
  35   │
  36   │ {% block scripts %}
  37   │ {{ super() }}
  38   │ <script>
  39   │     console.log('test-content');
  40   │ </script>
  41   │ {{ moment.include_moment() }}
  42   │ {{ moment.lang('zh-CN')}}
  43   │ {% endblock %}
───────┴────────────────────
» 转载请注明来源:呢喃 » flask 几个扩展应用

Leave a Reply

Your email address will not be published. Required fields are marked *

18 + fourteen =