# rahuldkjain/crop_prediction architecture

> Architecture diagram of the GitHub repository rahuldkjain/crop_prediction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rahuldkjain/crop_prediction
- Repository: https://github.com/rahuldkjain/crop_prediction

## Overview

##### Project type and purpose
- This is a full-stack web application built with Python and Flask.

##### File structure
- Entry point: `app.py` (Flask server routes, HTTP handlers).
- Business-logic / Model code: `crops.py` (loads CSV datasets in `static/*.csv`, trains or loads Decision-Tree regression models, produces predictions).
- Static assets:
- `static/*.csv` (time-series data per crop, rainfall, WPI)
- CSS/JS: MaterializeCSS + Chart.js included via templates.
- Templates:
- `templates/index.html` (home page, form input for crop selection/time window)
- `templates/commodity.html` (detailed crop pages)
- `templates/export` (probably CSV/JSON export endpoints).
- Dependencies: `requirements.txt` (Flask, scikit-learn, pandas, numpy).

##### Extract insights from the README
- Highlights Decision-Tree Regression with ~93–95% accuracy.
- Frontend uses MaterializeCSS (layout/components) and Chart.js (data visualization).
- Data source: data.gov.in (rainfall, WPI, crop pricing).
- Usage flow: user selects crop → server loads data/model → returns forecast → client renders table/chart.

##### Main components & interactions
a. Frontend (Browser)
- UI pages built with MaterializeCSS templates & Chart.js charts.
- Sends HTTP GET/POST to Flask endpoints.
b. Web Server (Flask app.py)
- Routing layer: receives requests (e.g., “/predict?crop=Maize”), calls controller functions.
- Renders templates with data or returns JSON for Chart.js.
c. Prediction Engine (crops.py)
- Data Loader: reads required CSV(s) from `static/`.
- Model Trainer/Loader: trains Decision-Tree Regressor or loads pre-trained model in memory.
- Prediction Logic: computes next-12-month forecasts, top gainers/losers.
d. Data Storage
- Flat files (`.csv` in `static/`).
- (Optional) In-memory caches or pickled models.
e. External Libraries
- scikit-learn (modeling), pandas/numpy (data manipulation), Chart.js (frontend charts), MaterializeCSS (UI).

##### Architectural patterns & design principles
- MVC-like separation:
- Model = `crops.py` data+prediction logic
- View = Jinja2 HTML templates + embedded JS
- Controller = Flask routes in `app.py`
- Single-process, synchronous request handling (simple Flask server).
- Static assets served directly by Flask’s static folder.
- Data-driven design: each crop CSV provides a consistent schema.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface"
        UI[/"User Interface"/]:::ui
        Templates["Templates"]:::ui
        Images["Images"]:::ui
        ChartJS["Chart.js"]:::ext
        Materialize["MaterializeCSS"]:::ext
    end

    %% Backend Layer
    subgraph "Flask Server"
        Server["app.py - Controllers & Routing"]:::server
    end

    subgraph "Prediction Module"
        Predict["crops.py - Data Loader & Model"]:::server
    end

    %% Data Storage
    DataStorage["static/*.csv - Time Series Datasets"]:::data

    %% External Libraries
    subgraph "External Libraries"
        SKL["scikit-learn"]:::ext
        PD["pandas & numpy"]:::ext
    end

    %% Connections
    UI -->|HTTP GET/POST| Server
    Server -->|calls| Predict
    Predict -->|load CSV| DataStorage
    Server -->|returns HTML/JSON| UI
    UI -->|renders charts| ChartJS
    UI -->|applies styles| Materialize
    Predict -->|uses| SKL
    Predict -->|uses| PD

    %% Click Events
    click UI "https://github.com/rahuldkjain/crop_prediction/blob/master/templates/index.html"
    click Templates "https://github.com/rahuldkjain/crop_prediction/tree/master/templates/"
    click Images "https://github.com/rahuldkjain/crop_prediction/tree/master/static/images/"
    click Server "https://github.com/rahuldkjain/crop_prediction/blob/master/app.py"
    click Predict "https://github.com/rahuldkjain/crop_prediction/blob/master/crops.py"
    click DataStorage "https://github.com/rahuldkjain/crop_prediction/tree/master/static/"
    click ChartJS "https://github.com/rahuldkjain/crop_prediction/blob/master/templates/index.html"
    click Materialize "https://github.com/rahuldkjain/crop_prediction/blob/master/templates/index.html"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085;
    classDef server fill:#d4edda,stroke:#155724,color:#155724;
    classDef data fill:#fff3cd,stroke:#856404,color:#856404;
    classDef ext fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
```
