0% found this document useful (0 votes)
9 views3 pages

Flask Dashboard Creation Steps

The document outlines the setup procedure for a Flask project named 'flask_dashboard', including configuration for database connection, model definition, and database initialization. It provides step-by-step instructions for creating the database, inserting JSON data into a MySQL table, and running the Flask server. Additionally, it mentions the need to create API routes to fetch and display data in JSON format.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Flask Dashboard Creation Steps

The document outlines the setup procedure for a Flask project named 'flask_dashboard', including configuration for database connection, model definition, and database initialization. It provides step-by-step instructions for creating the database, inserting JSON data into a MySQL table, and running the Flask server. Additionally, it mentions the need to create API routes to fetch and display data in JSON format.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Procedure setup for the project "flask_dashboard":

1) config.py

import os

DB_HOST = "localhost" # Change if using a remote DB


DB_USER = "root" # Your MySQL username
DB_PASSWORD = "@Abhinavsql123" # Your MySQL password
DB_NAME = "dashboard_db" # Your database name
DB_PORT = 3306 # Default MySQL port

2) models.py

from main import db

# Define a model based on JSON structure


class DataEntry(db.Model):
id = db.Column(db.Integer, primary_key=True)
end_year = db.Column(db.String(10))
intensity = db.Column(db.Integer)
sector = db.Column(db.String(255))
topic = db.Column(db.String(255))
insight = db.Column(db.Text)
url = db.Column(db.String(500))
region = db.Column(db.String(255))
start_year = db.Column(db.String(10))
impact = db.Column(db.String(10))
added = db.Column(db.String(50))
published = db.Column(db.String(50))
country = db.Column(db.String(255))
relevance = db.Column(db.Integer)
pestle = db.Column(db.String(255))
source = db.Column(db.String(255))
title = db.Column(db.Text)
likelihood = db.Column(db.Integer)

def __repr__(self):
return f"<DataEntry {self.title}>"

3) main.py

from flask import Flask


from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:
%40Abhinavsql123@localhost/dashboard_db"

app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app) # ✅ Initialize db after app

if __name__ == "__main__":
from models import DataEntry
app.run(debug=True)
4) init_db.py

from main import app, db


from models import DataEntry

with app.app_context():
db.create_all()
print("✅ Database tables created successfully!")

5) ## Open MySQL and create the database in the SQL commandline client

--> CREATE DATABASE dashboard_db;

6) ## Now create the table named "DataEntry" using this script in the vscode
terminal

--> python init_db.py

7) ## Check in the MySQL commandline client whether the table is created or not

--> USE dashboard_db;


--> DESC data_entry;

8) Run and check whether the flask server is running

--> python main.py

9) ## Since the table is created, we have to insert json data into the table

--> data_loader.py(code):

import json
from main import db, app
from models import DataEntry

# Load JSON data from the file


with open("jsondata.json", "r", encoding="utf-8") as file:
data = json.load(file) # Parse JSON file

# Function to convert empty strings to None (NULL in MySQL)


def clean_value(value, is_int=False):
if value == "" or value is None:
return None
return int(value) if is_int else value # Convert to int if required

# Insert data into MySQL


with app.app_context():
for entry in data:
new_entry = DataEntry(
end_year=clean_value(entry.get("end_year")),
intensity=clean_value(entry.get("intensity"), is_int=True),
sector=clean_value(entry.get("sector")),
topic=clean_value(entry.get("topic")),
insight=clean_value(entry.get("insight")),
url=clean_value(entry.get("url")),
region=clean_value(entry.get("region")),
start_year=clean_value(entry.get("start_year")),
impact=clean_value(entry.get("impact")),
added=clean_value(entry.get("added")),
published=clean_value(entry.get("published")),
country=clean_value(entry.get("country")),
relevance=clean_value(entry.get("relevance"), is_int=True),
pestle=clean_value(entry.get("pestle")),
source=clean_value(entry.get("source")),
title=clean_value(entry.get("title")),
likelihood=clean_value(entry.get("likelihood"), is_int=True)
)
db.session.add(new_entry) # Add to session

db.session.commit() # Commit all changes


print("✅ JSON Data Successfully Inserted into MySQL!")

10) ## Now run this command to load the json data successfully

--> python data_loader.py

11) Check whether the data is inserted or not

--> USE dashboard_db;


--> SELECT insight FROM data_entry LIMIT 5;

12) ## Now we have to create Flask API routes to fetch and display the data in JSON
format.

You might also like