Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive Data Visualizations tool #791

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tools/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from flask import Flask, render_template, request, redirect, url_for
import os
import pandas as pd
from visualizations import plots

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'

# Ensure the upload folder exists
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])

# Route for the home page
@app.route('/')
def index():
return render_template('index.html')

# Route to handle dataset uploads and generate visualizations
@app.route('/upload', methods=['POST'])
def upload_file():
# Check if a file is included in the request
if 'file' not in request.files:
return redirect(url_for('index'))

file = request.files['file']
if file.filename == '':
return redirect(url_for('index'))

# Save the uploaded file
if file:
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)

# Read dataset and generate visualizations
try:
dataset = pd.read_csv(filepath)
except Exception as e:
return f"Error reading file: {e}"

plots_data = plots.generate_plots(dataset)

# Pass generated plots to the template
return render_template('index.html', plots=plots_data)

if __name__ == '__main__':
app.run(debug=True)
5 changes: 5 additions & 0 deletions tools/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Flask==2.0.1
matplotlib==3.4.3
plotly==5.3.1
pandas==1.3.3
seaborn==0.11.2
17 changes: 17 additions & 0 deletions tools/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
}

h1, h2, h3 {
color: #333;
}

#plots-container {
display: flex;
flex-direction: column;
}

#plots-container div {
margin-top: 20px;
}
33 changes: 33 additions & 0 deletions tools/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ML Nexus - Data Visualization</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>Upload Your Dataset</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept=".csv">
<button type="submit">Upload</button>
</form>

{% if plots %}
<h2>Visualizations</h2>
<div id="plots-container">
{% if plots.scatter_plot %}
<div>
<h3>Scatter Plot</h3>
{{ plots.scatter_plot|safe }}
</div>
{% endif %}
{% if plots.correlation_heatmap %}
<div>
<h3>Correlation Heatmap</h3>
<img src="{{ plots.correlation_heatmap }}" alt="Correlation Heatmap">
</div>
{% endif %}
</div>
{% endif %}
</body>
</html>
33 changes: 33 additions & 0 deletions tools/visualizations/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
import io
import base64

def generate_plots(dataset):
plots = {}

# Scatter Plot for first two columns if numeric
if dataset.select_dtypes(include='number').shape[1] >= 2:
numeric_columns = dataset.select_dtypes(include='number').columns[:2]
fig = px.scatter(dataset, x=numeric_columns[0], y=numeric_columns[1])
plots['scatter_plot'] = fig.to_html(full_html=False)

# Correlation heatmap for all numeric columns
correlation_matrix = dataset.corr()
plt.figure(figsize=(8, 6))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', cbar=True)
heatmap = save_matplotlib_to_base64()
plots['correlation_heatmap'] = heatmap

return plots

# Helper function to save Matplotlib figures as base64 strings
def save_matplotlib_to_base64():
buf = io.BytesIO()
plt.savefig(buf, format="png")
plt.close()
buf.seek(0)
img_base64 = base64.b64encode(buf.getvalue()).decode("utf-8")
return f'data:image/png;base64,{img_base64}'
Loading