Answer the question
In order to leave comments, you need to log in
How to import everything from a python file?
Good evening, I have quite a lot of code in my project (Flask), and I'm afraid that very soon it will become unreadable, so, I tried to move part of it into a separate file, and flask simply does not see it, how to solve this problem?
config.py
import os, telebot
from flask import Flask, session, render_template, jsonify, request, redirect
from site import *
app = Flask(__name__)
app.secret_key = 'hello'
token = '***************************'
bot = telebot.TeleBot(token)
if __name__ == '__main__':
print(colored(' * App is running!', 'green'))
print()
app.run(debug = True)
@app.route('/')
def index():
if session.get('logged'):
return redirect('/homepage')
else:
return redirect('/submit')
@app.route('/submit', methods=['GET', 'POST'])
def login():
if not session.get('logged'):
if request.method == 'POST':
key = request.form['key']
if key == token:
session['logged'] = True
else:
return redirect('/')
return render_template('submit.html', title = 'Sign Up!')
@app.route('/logout')
def logout():
session.clear()
return redirect('/')
@app.route('/homepage')
def homepage():
if session.get('logged'):
return render_template('home.html', title = 'Bot')
else:
return redirect('/')
Answer the question
In order to leave comments, you need to log in
For example, here are a few ways to do
# структура проекта
├── api.py
├── app.py
├── config.py
├── __init__.py
├── start.sh
├── templates
├── venv
└── views.py
# api.py
from flask import Blueprint, jsonify
api = Blueprint('api', __name__)
@api.route('/')
def index():
return jsonify({'api': {'version': 1.1}})
# app.py
from flask import Flask
from .views import blog
from .api import api
app = Flask(__name__)
app.config.from_pyfile('config.py')
app.register_blueprint(blog)
app.register_blueprint(api, url_prefix='/api/v1')
# views.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
blog = Blueprint('blog', __name__, template_folder='templates')
@blog.route('/')
def show():
try:
return render_template('index.html')
except TemplateNotFound:
abort(404)
# start.sh
#!/bin/sh
. venv/bin/activate
export FLASK_ENV=development
python -m flask run
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question