You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB
Python

# app.py
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from apscheduler.schedulers.background import BackgroundScheduler
import importlib.util
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@postgres:5432/db_name'
db = SQLAlchemy(app)
scheduler = BackgroundScheduler()
class Schedule(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
time = db.Column(db.String(255))
task = db.Column(db.String(255))
@app.route('/')
def index():
schedules = Schedule.query.all()
return render_template('index.html', schedules=schedules)
@app.route('/create_schedule', methods=['POST'])
def create_schedule():
name = request.form['name']
time = request.form['time']
task = request.form['task']
new_schedule = Schedule(name=name, time=time, task=task)
db.session.add(new_schedule)
db.session.commit()
module = importlib.import_module('tasks')
func = getattr(module, task)
scheduler.add_job(func=func, trigger='cron', hour=time.hour, minute=time.minute)
return redirect('/')
if __name__ == "__main__":
scheduler.start()
app.run(host='0.0.0.0', port=5000)