0
votes

Following microblog tutorial on Flask: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

In Pycharm, no matter how I structure or name the files, I cannot get the dev server to run if I separate the code and import the files. I also can't get it to inherit the codes no matter where I move the init, views, run files. The only way for me to get the server to run is to have all the commands execute on the same file. What am I doing wrong?

I have it setup as: Project 1 > app(directory) > tmp(directory) > run.py(file)

app(directory) > static(directory) > templates(directory) > init.py(file) > views.py(file) (I have tried different arrangements.)

Inside views.py: from app import app

Inside run.py: from app import app

Inside init.py: from flask import Flask from app import views

(I have tried many different combinations such as from app import app.views. from app import views as app_views. I have also tried renaming the directories/files, nothing is working.)

1
It's really hard to tell what you're doing. Does "app(directory) > tmp(directory) > run.py(file)" mean that you have app/tmp/run.py? Or does it mean that app, tmp, and run.py all live inside the same directory? You may also want to include contents of your files so that people can try to recreate the problem.dirn
@dirn -(Inside Directory and Project file) Project1/run.py Project1/tmp Project 1/app (Inside app directory) Project1/app/__init__.py Project1/app/views.py Project1/app/static Project1/app/templatesJohnathan
Please update the question. Comments are well suited for this type of content.dirn

1 Answers

1
votes
  1. Build the new project with PyCharm, it will create a virtual environment for you. Then put these into a run.py in a root of your a project like that (don't forget to turn debugging mode off in prod)

        from app import create_app
        app = create_app()
    
        if __name__ == '__main__':
             app.run(debug=True)
    
    1. Set up init.py file inside you 'app':

      def create_app(config_class=Config):
          app = Flask(__name__)
          app.config.from_object(Config)
          db.init_app(app)
          bcrypt.init_app(app)
          login_manager.init_app(app)
          mail.init_app(app)
      
    2. Store your credentials into Config class:

      class Config: SECRET_KEY = 'your key... ' SQLALCHEMY_DATABASE_URI = 'your db...' SQLALCHEMY_TRACK_MODIFICATIONS = False MAIL_SERVER = 'smtp.google.com' MAIL_PORT = 587 MAIL_USE_TLS = True MAIL_USERNAME = 'your email' MAIL_PASSWORD = 'email password'

    3. Structure your project, placing an empty init.py into each directory( accordingly to your architecture). Here is an example below, how to structure your project in Flask. It runs with no problem on

    . ├── README.md ├── app │   ├── __init__.py │   ├── config.py │   ├── errors │   │   ├── 403.html │   │   ├── 404.html │   │   ├── 500.html │   │   ├── __init__.py │   │   └── handlers.py │   ├── main │   │   ├── __init__.py │   │   └── routes.py │   ├── models.py │   ├── posts │   │   ├── __init__.py │   │   ├── forms.py │   │   └── routes.py │   ├── site.db │   ├── static │   │   ├── main.css │   │   └── profile_pics │   │   ├── 3c4feb2bb50d90df.png │   │   ├── ba3d328163a8125e.png │   │   └── default.jpg │   ├── templates │   │   ├── about.html │   │   ├── account.html │   │   ├── create_post.html │   │   ├── home.html │   │   ├── layout.html │   │   ├── login.html │   │   ├── post.html │   │   ├── register.html │   │   ├── reset_request.html │   │   ├── reset_token.html │   │   └── user_posts.html │   └── users │   ├── __init__.py │   ├── forms.py │   ├── routes.py │   └── utils.py └── run.py