Created
April 28, 2019 08:44
-
-
Save jufianto/8a41ffe2ba53eb2c3571f7a6d0702d5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code by Jufi | |
| # Do Not Remove This | |
| import os, time | |
| from time import sleep | |
| from flask import Flask, flash, redirect, render_template, url_for, request, json, make_response, jsonify, Response | |
| from flask_migrate import Migrate | |
| from flask_sqlalchemy import SQLAlchemy | |
| from config import app_config | |
| config_name = os.getenv('FLASK_CONFIG') or 'default' | |
| db = SQLAlchemy() | |
| app = Flask(__name__, instance_relative_config=True) | |
| app.config.from_object(app_config[config_name]) | |
| app.config.from_pyfile('config.py') | |
| db.init_app(app) | |
| migrate = Migrate(app, db) | |
| from twt import models | |
| # def stream_template(template_name, **context): | |
| # app.update_template_context(context) | |
| # t = app.jinja_env.get_template(template_name) | |
| # rv = t.stream(context) | |
| # rv.enable_buffering(5) | |
| # return rv | |
| def stream_template(template_name, **context): | |
| app.update_template_context(context) | |
| t = app.jinja_env.get_template(template_name) | |
| rv = t.stream(context) | |
| rv.disable_buffering() | |
| return rv | |
| data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] | |
| def generate(): | |
| for item in data: | |
| yield str(item) | |
| sleep(1) | |
| @app.route('/stream') | |
| def stream_view(): | |
| rows = generate() | |
| return Response(stream_template('text.html', rows=rows)) | |
| @app.route('/') | |
| def blank(): | |
| return render_template('base.html', title="Hello") | |
| @app.route('/download', methods=['GET', 'POST']) | |
| def download(): | |
| return render_template('form.html') | |
| @app.route('/api/download', methods=['GET', 'POST']) | |
| def simpan(): | |
| if request.method == 'POST': | |
| keyword = Log(text=request.form.get('keyword')) | |
| db.session.add(keyword) | |
| db.session.commit() | |
| data = Log.query.order_by(Log.id).all() | |
| list = [r.as_dict() for r in data] | |
| resp = jsonify(list) | |
| resp.status_code = 200 | |
| return resp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code by Jufi | |
| # Do Not Remove This | |
| from twt import db | |
| class Log(db.Model): | |
| __tablename = 'log' | |
| id = db.Column(db.Integer, primary_key=True) | |
| text = db.Column(db.String(60), index=True) | |
| def __repr__(self): | |
| return '<Log: {}>'.format(self.text) | |
| def as_dict(self): | |
| return {'name': self.text} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment