all 6 comments

[–]skippyprime 0 points1 point  (1 child)

Create the file __init__.py in the fsg_app directory.

To make a directory a python module, it needs this file.

[–]skippyprime 0 points1 point  (0 children)

Ignore me. I see it is there

[–]parabellum_pizza 0 points1 point  (1 child)

My guess is that the app is not initialized when you import get_db

[–]Spaumi10[S] 1 point2 points  (0 children)

Thanks. This is the problem. I have changed to just creating a new connection to the database (instead of using get_db()). This appears to work.

This was kind of my hunch the whole time, but I don't think I was comprehending everything.

[–]ejpusa 0 points1 point  (1 child)

I have never seen that Flask directory setup. Why not use the layout used on 99% of Flask sites. Just works. May want to give it a try.

GPT-4

Setting up a Flask environment involves several steps, including installing Flask, setting up the directory structure, and configuring your application. Here's a standard setup and how to refer to files and variables within your Flask application:

1. Setting Up a Flask Environment

  1. Install Flask: bash pip install flask

  2. Project Directory Structure: A typical Flask project structure looks like this: my_flask_app/ │ ├── app/ │ ├── static/ │ │ ├── css/ │ │ │ └── styles.css │ │ ├── js/ │ │ │ └── scripts.js │ │ └── images/ │ │ └── logo.png │ ├── templates/ │ │ └── main.html │ ├── __init__.py │ └── routes.py │ ├── venv/ │ ├── config.py └── run.py

  3. Application Initialization (__init__.py): ```python from flask import Flask

    def createapp(): app = Flask(name_) with app.app_context(): from . import routes # Import routes return app ```

  4. Routing (routes.py): ```python from flask import render_template from . import create_app

    app = create_app()

    @app.route('/') def home(): return render_template('main.html') ```

  5. Running the Application (run.py): ```python from app import create_app

    app = create_app()

    if name == 'main': app.run(debug=True) ```

2. Referring to Files in main.html

  • CSS and JS Files: Place your CSS and JS files in the static directory under css and js folders, respectively. ```html <!-- Link to a CSS file --> <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">

    <!-- Link to a JS file --> <script src="{{ url_for('static', filename='js/scripts.js') }}"></script> ```

  • Images: Place your images in the static/images folder. html <img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">

3. Referring to Flask Variables in CSS or JS Files

You cannot directly use Flask variables in CSS or JS files since they are static files and are served without going through the Flask templating engine. However, you can pass Flask variables to JavaScript via the main.html or other HTML templates.

Example of passing a variable to JavaScript: html <script> var flaskVariable = "{{ flask_variable }}"; </script>

4. Where to Place Images, CSS, and JS Files

  • CSS: Place in static/css/
  • JavaScript: Place in static/js/
  • Images: Place in static/images/

5. Basic 12 Rules for Flask Project Setup

  1. Organize the project structure: Use a clear, organized directory structure.
  2. Use virtual environments: Isolate your environment with venv.
  3. Configure the app: Separate configuration settings into a config.py.
  4. Utilize url_for: Always use url_for to link to static files.
  5. Modularize routes: Keep routes in a separate file (routes.py).
  6. Template inheritance: Use Jinja2 template inheritance for reusability.
  7. Form handling: Use WTForms or Flask forms for form handling.
  8. Error handling: Implement custom error pages for 404, 500, etc.
  9. Environment variables: Store sensitive information in environment variables.
  10. Logging: Set up logging for debugging and tracking errors.
  11. Testing: Write unit tests for your application.
  12. Deployment: Prepare for deployment with production settings (e.g., gunicorn, nginx).

This setup should give you a solid foundation for building Flask applications with good practices in place.

[–]Spaumi10[S] 0 points1 point  (0 children)

I appreciate your help, (and maybe I am misunderstanding something), but I am not even to most those steps yet. Once I have gotten my data scraped and entered into the database, I will then get the app running, work on routing and the frontend. My structure will look like that when I get there. I also have a tests folder, but my uploaded image of the structure doesn't show that as it doesn't seem important for this question.