# sunvtoan/book-review-website-flask architecture

> Architecture diagram of the GitHub repository sunvtoan/book-review-website-flask, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sunvtoan/book-review-website-flask
- Repository: https://github.com/sunvtoan/book-review-website-flask

## Overview

##### Main Components
- Frontend:
– The “templates” directory (HTML files for index, details, login, register, etc.) and “static” folder (CSS, SCSS files) represent the presentation layer built with HTML, CSS, and Jinja templating.
- Backend:
– The main Flask application (application.py) handles routing, authentication (registration, login, logout), API integration, and interactions with external services.
– Scripts like crawl.py (for using Beautiful Soup 4 to crawl Amazon reviews) and import.py (to import CSV data into the database) reside here.
- Database:
– The “tables.sql” file indicates a PostgreSQL database setup.
– CSV files (books.csv, reviews1.csv, bookscopy.csv) are sources for data imported via import.py.
- External Services / APIs:
– Integration with the Open Library API is used to fetch book data (cover images, ratings, etc.).
– The project also interacts with an external website (Amazon) through web scraping to gather additional review data.
- Session Management:
– The “flask_session” folder stores session data, showing that the project uses server-side session management.
- Environment and Deployment:
– Files such as .env, Procfile, and requirements.txt imply considerations for environment configuration and deployment (possibly on platforms like Heroku).

##### Relationships and Interactions
- Users interact with the frontend (web pages rendered using Jinja) by registering, logging in, searching for books, and submitting reviews.
- These actions trigger HTTP requests to the Flask backend (application.py) where:
– Business logic processes the request.
– For user authentication, the backend handles form submissions and session management.
– For book searches or details, the backend queries the Postgres database (set up using tables.sql) and enriches this data by calling the Open Library API.
– The backend may also run the crawl.py process (or similar functionality) to scrape additional data from external sources when needed.
- Data Import:
– The import.py script is used initially (or periodically) to load book data from CSV files into the PostgreSQL database.
- Data Flow:
– Data flows from external APIs and CSV sources into the database, then from the database to the backend, and finally to the user interface on the frontend.
- Sessions:
– Session management is handled by Flask and stored in the flask_session directory, maintaining user state between requests.

##### Important Architectural Patterns and Technologies
- MVC (or similar clear separation) – with the “Model” represented by the database and data import scripts, the “View” in the templates/static, and the “Controller” in the Flask routes (application.py) managing the logic.
- Integration of third-party APIs (Open Library API) and web scraping for additional content.
- Use of file-based session management (flask_session), which suggests a design decision to persist session data beyond a single request.
- Technologies: Python (Flask), PostgreSQL, Beautiful Soup for web scraping, and standard web technologies (HTML, CSS, Jinja).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    User["User"]:::user

    %% Frontend Layer
    subgraph "Frontend Layer"
        frontTemplates["Templates (HTML/Jinja)"]:::frontend
        frontStatic["Static Assets (CSS/SCSS)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        app["Flask Application (application.py)"]:::backend
        crawl["Web Scraping Service (crawl.py)"]:::backend
        importer["Data Import Script (import.py)"]:::backend
        session["Session Management (flask_session/)"]:::backend
    end

    %% Data Layer
    subgraph "Data Layer"
        db["PostgreSQL Database (tables.sql)"]:::database
        subgraph "CSV Data Sources"
            csv1["books.csv"]:::database
            csv2["reviews1.csv"]:::database
            csv3["bookscopy.csv"]:::database
        end
    end

    %% External Services
    subgraph "External Services"
        openLib["Open Library API"]:::external
        amazon["Amazon Reviews"]:::external
    end

    %% Deployment & Environment
    subgraph "Deployment & Environment Configuration"
        env[".env"]:::deployment
        proc["Procfile"]:::deployment
        req["requirements.txt"]:::deployment
    end

    %% Relationships
    User -->|"uses"| frontTemplates
    User -->|"loads"| frontStatic
    frontTemplates -->|"HTTP Request"| app
    frontStatic -->|"Assets Served"| app

    app <-->|"CRUD Operations"| db
    csv1 -->|"Data Import"| importer
    csv2 -->|"Data Import"| importer
    csv3 -->|"Data Import"| importer
    importer -->|"Populate"| db

    app -->|"Fetch data"| openLib
    app -->|"Trigger scraping"| crawl
    crawl -->|"Scrape reviews from"| amazon

    app -->|"Manage Session"| session

    env ---|"Configuration"| app
    proc ---|"Deployment"| app
    req ---|"Dependencies"| app

    %% Styles
    classDef user fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef frontend fill:#b3d9ff,stroke:#0052cc,stroke-width:2px;
    classDef backend fill:#d2f8d2,stroke:#0a7f0a,stroke-width:2px;
    classDef database fill:#f2f2f2,stroke:#999999,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#b58900,stroke-width:2px;
    classDef deployment fill:#ffd9b3,stroke:#cc6600,stroke-width:2px;

    %% Click Events
    click frontTemplates "https://github.com/sunvtoan/book-review-website-flask/tree/master/templates/"
    click frontStatic "https://github.com/sunvtoan/book-review-website-flask/tree/master/static/"
    click app "https://github.com/sunvtoan/book-review-website-flask/blob/master/application.py"
    click crawl "https://github.com/sunvtoan/book-review-website-flask/blob/master/crawl.py"
    click importer "https://github.com/sunvtoan/book-review-website-flask/blob/master/import.py"
    click db "https://github.com/sunvtoan/book-review-website-flask/blob/master/tables.sql"
    click csv1 "https://github.com/sunvtoan/book-review-website-flask/blob/master/books.csv"
    click csv2 "https://github.com/sunvtoan/book-review-website-flask/blob/master/reviews1.csv"
    click csv3 "https://github.com/sunvtoan/book-review-website-flask/blob/master/bookscopy.csv"
    click session "https://github.com/sunvtoan/book-review-website-flask/tree/master/flask_session/"
    click env "https://github.com/sunvtoan/book-review-website-flask/blob/master/.env"
    click proc "https://github.com/sunvtoan/book-review-website-flask/tree/master/Procfile"
    click req "https://github.com/sunvtoan/book-review-website-flask/blob/master/requirements.txt"
```
