-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6c5158
commit 2c89114
Showing
1 changed file
with
32 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,52 @@ | ||
from flask import Flask, render_template, request | ||
from pyspark.sql import SparkSession | ||
import os | ||
from flask import Flask, render_template, request, jsonify | ||
import pandas as pd | ||
import plotly.express as px | ||
import json | ||
from plotly.subplots import make_subplots | ||
import plotly.graph_objects as go | ||
import plotly # Add this line to import the plotly module | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/', methods=['GET', 'POST']) | ||
def index(): | ||
return render_template('index.html') | ||
df = None | ||
pie_data = None | ||
bar_data = None | ||
chart_data = None | ||
|
||
@app.route('/uploadfile', methods=['POST']) | ||
def uploadfile(): | ||
if request.method == 'POST': | ||
file = request.files['file'] | ||
if file.filename == '': | ||
return 'No selected file' | ||
if file: | ||
filename = file.filename | ||
file.save(filename) | ||
df = pd.read_csv(file) | ||
if 'Department_Name' not in df.columns: | ||
return 'Error: Department_Name column not found in CSV file' | ||
|
||
# Inisialisasi Spark Session | ||
spark = SparkSession.builder \ | ||
.appName('Tools Data Analyst and Business Analyst') \ | ||
.getOrCreate() | ||
pie_data = df['Department_Name'].value_counts() | ||
bar_data = df.groupby('Department_Name')['Division'].sum().reset_index() | ||
|
||
# Load file CSV menjadi DataFrame Spark | ||
df = spark.read.option('header', 'true').csv(filename) | ||
pie_chart = px.pie(pie_data, values=pie_data.values, names=pie_data.index, title='Pie Chart') | ||
bar_chart = px.bar(bar_data, x='Department_Name', y='Division', title='Bar Chart') | ||
|
||
# Menghitung jumlah data kategorikal untuk chart Pie | ||
pie_data = df.groupBy('Category').count().collect() | ||
pie_data = [(row['Category'], row['count']) for row in pie_data] | ||
pie_chart_title = pie_chart.layout.title.text | ||
pie_chart_layout = pie_chart.layout.to_plotly_json() | ||
|
||
# Menghitung jumlah data numerikal untuk chart Bar | ||
bar_data = df.groupBy('Category').agg({'Value': 'sum'}).collect() | ||
bar_data = [(row['Category'], row['sum(Value)']) for row in bar_data] | ||
bar_chart_title = bar_chart.layout.title.text | ||
bar_chart_layout = bar_chart.layout.to_plotly_json() | ||
|
||
# Menghapus file CSV yang diunggah | ||
os.remove(filename) | ||
# Combine pie and bar charts into one plot | ||
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.05, specs=[[{"type": "domain"}], [{"type": "bar"}]]) | ||
|
||
return render_template('index.html', pie_data=pie_data, bar_data=bar_data) | ||
fig.add_trace(go.Pie(labels=pie_data.index, values=pie_data.values, name='Pie Chart'), row=1, col=1) | ||
fig.add_trace(go.Bar(x=bar_data['Department_Name'], y=bar_data['Division'], name='Bar Chart'), row=2, col=1) | ||
|
||
fig.update_layout(title_text='Pie and Bar Charts', height=800, width=1000) | ||
|
||
chart_data = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) | ||
|
||
return render_template('index.html', df=df, chart_data=chart_data) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |