S
S
Spoon in the brain2019-06-08 23:43:20
Python
Spoon in the brain, 2019-06-08 23:43:20

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)

site.py
@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('/')

In fact, I have been suffering for a long time, I probably need to go and learn the basics, but the fact is that I successfully managed to import some files, and everything worked fine. But here with a flask something does not leave. So, how to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2019-06-09
@vessels

For example, here are a few ways to do

We look carefully
# структура проекта
├── 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

D
Danil K., 2019-06-09
@Danya_Violet

Have you tried renaming the site.py file? Perhaps there is such a module, and therefore it is not imported

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question