Code Monkey home page Code Monkey logo

Comments (7)

miguelgrinberg avatar miguelgrinberg commented on May 22, 2024

The topic list is now closed, maybe if I do another round of updates at some point. But I honestly don't see much educational value in showing how to use Flask-Admin, while the extension can be useful for some types of applications, it does not really teach any new concepts. My suggestion is that you fork the project and work on adding it yourself as an exercise.

from microblog.

doaa-altarawy avatar doaa-altarawy commented on May 22, 2024

Thanks. Would you accept pull requests on this repository (if it meets the coding style)?

from microblog.

doaa-altarawy avatar doaa-altarawy commented on May 22, 2024

Also, Flask_Admin is a good example of circular import in multi-file flask projects (models needs db, admin needs models, and create_app register Admin_views)

from microblog.

miguelgrinberg avatar miguelgrinberg commented on May 22, 2024

I can't accept it on this repository because it will be out of sync with the tutorial, but I can leave the PR here for others that are interested in seeing how you integrated it.

from microblog.

crhuber avatar crhuber commented on May 22, 2024

@doaa-altarawy

Did you ever resolve this ? Can you show how you managed to get around circular import ?

from microblog.

doaa-altarawy avatar doaa-altarawy commented on May 22, 2024

Yes, I did. I've flask_admin working well with this microblog (or flasky) as the base template for the structure.
in app/__init__.py (where you have the create_app function), don't import your admin package on the top, rather, import it inside the create_app function. Here is an example:

app/__init__.py:

from flask import Flask
from config import config
from flask_mongoengine import MongoEngine
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from flask_admin import Admin
from flask_login import LoginManager
from flask_debugtoolbar import DebugToolbarExtension
from flask_moment import Moment


mail = Mail()
db = MongoEngine()
bootstrap = Bootstrap()
app_admin = Admin(name='My admin name', template_mode='bootstrap3',
                  base_template='admin/custom_base.html')

login_manager = LoginManager()
login_manager.login_view = 'auth.login' 
moment = Moment()
toolbar = DebugToolbarExtension()


def create_app(config_name):
 
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    # init
    mail.init_app(app)
    db.init_app(app)
    bootstrap.init_app(app)
    login_manager.init_app(app)
    app_admin.init_app(app)
    moment.init_app(app)
    toolbar.init_app(app)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    # To avoid circular import
    from app.admin import add_admin_views
    add_admin_views()

    return app

Then in the app/admin.py:

def add_admin_views():
    """Register views to admin"""

    from .. import app_admin
    app_admin.add_view(SoftwareView(Software, name='Software List'))
    app_admin.add_view(UserView(User, name='Users'))

from microblog.

SebastianOpiyo avatar SebastianOpiyo commented on May 22, 2024

@doaa-altarawy, seems a good way to handle the problem, I am so convinced before I try it out. Thanks.

from microblog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.