1. Import file datasheet yang akan dipakai ke MongoDB menggunakkan VS Code. pastikan file sudah csv dan nama sesuai dengan nama file. buat file pada VS Code dengan nama import_csv.py
2. Untuk menampilkan data dan fungsi CRUD, buat file app.py lalu isi dengan kode seperti dibawah ini :
# import csv
from flask import Flask, render_template, request, redirect
from pymongo import MongoClient
from bson import ObjectId
import plotly.graph_objects as go
app = Flask(__name__)
client = MongoClient('mongodb://localhost:27017/')
db = client['pbl']
koleksi = db['dataGYM']
# Membaca file CSV dan memasukkan data ke MongoDB jika belum ada
# path_csv = 'ds_salaries.csv'
# with open(path_csv, 'r') as csvfile:
# csv_reader = csv.DictReader(csvfile)
# for row in csv_reader:
# koleksi.insert_one(row)
# Rute untuk menampilkan data dari koleksi MongoDB
# @app.route('/')
def index():
data_cursor = koleksi.find()
formatted_data = []
header = ['number','title','desc','type','bodypart','equipment','level','rating','ratingdesc']
for row in data_cursor:
formatted_row = {key: row[key] for key in header}
formatted_data.append(formatted_row)
return render_template('index.html', data=formatted_data)
# Rute lainnya untuk tambah, edit, dan hapus data
# Rute untuk menampilkan data dan melakukan pencarian
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
query = request.form.get('query')
if query:
data_cursor = koleksi.find({
'$or': [
{'number': {'$regex': query, '$options': 'i'}},
{'title': {'$regex': query, '$options': 'i'}},
{'desc': {'$regex': query, '$options': 'i'}},
{'type': {'$regex': query, '$options': 'i'}},
{'bodypart': {'$regex': query, '$options': 'i'}},
{'equipment': {'$regex': query, '$options': 'i'}},
{'level': {'$regex': query, '$options': 'i'}},
{'rating': {'$regex': query, '$options': 'i'}},
{'ratingdesc': {'$regex': query, '$options': 'i'}},
]
})
data_list = list(data_cursor)
return render_template('index.html', data=data_list)
else:
return redirect('/')
else:
data_cursor = koleksi.find()
data_list = list(data_cursor)
return render_template('index.html', data=data_list)
@app.route('/graph')
def show_graph():
# Menghitung jumlah perusahaan berdasarkan type
type_counts = koleksi.aggregate([
{"$group": {"_id": "$type", "count": {"$sum": 1}}}
])
# Menghitung jumlah perusahaan berdasarkan bodypart
bodypart_counts = koleksi.aggregate([
{"$group": {"_id": "$bodypart", "count": {"$sum": 1}}}
])
equipment_counts = koleksi.aggregate([
{"$group": {"_id": "$equipment", "count": {"$sum": 1}}}
])
level_counts = koleksi.aggregate([
{"$group": {"_id": "$level", "count": {"$sum": 1}}}
])
# Menghitung jumlah perusahaan berdasarkan rating
rating_counts = koleksi.aggregate([
{"$group": {"_id": "$rating", "count": {"$sum": 1}}}
])
# Membuat dictionary untuk variabel title, year_founded, dan bodypart
type = {item['_id']: item['count'] for item in type_counts}
bodypart = {item['_id']: item['count'] for item in bodypart_counts}
equipment = {item['_id']: item['count'] for item in equipment_counts}
level = {item['_id']: item['count'] for item in level_counts}
rating = {item['_id']: item['count'] for item in rating_counts}
# Membuat grafik batang untuk type
fig_type = go.Figure()
fig_type.add_trace(go.Bar(x=list(type.keys()), y=list(type.values()), marker=dict(color='coral', opacity=0.7)))
fig_type.update_layout(title='Tipe Exercise', xaxis_title='type', yaxis_title='Jumlah')
# Membuat grafik batang untuk bodypart
fig_bodypart = go.Figure()
fig_bodypart.add_trace(go.Bar(x=list(bodypart.keys()), y=list(bodypart.values()), marker=dict(color='darkseagreen', opacity=0.7)))
fig_bodypart.update_layout(title='Bagian Tubuh', xaxis_title='bodypart', yaxis_title='Jumlah')
fig_equipment = go.Figure()
fig_equipment.add_trace(go.Bar(x=list(equipment.keys()), y=list(equipment.values()), marker=dict(color='peachpuff', opacity=0.7)))
fig_equipment.update_layout(title='Alat yang digunakan', xaxis_title='equipment', yaxis_title='Jumlah')
fig_level = go.Figure()
fig_level.add_trace(go.Bar(x=list(level.keys()), y=list(level.values()), marker=dict(color='lightblue', opacity=0.7)))
fig_level.update_layout(title='level', xaxis_title='level', yaxis_title='Jumlah')
# Membuat grafik batang untuk rating
fig_rating = go.Figure()
fig_rating.add_trace(go.Bar(x=list(rating.keys()), y=list(rating.values()), marker=dict(color='lightgreen', opacity=0.7)))
fig_rating.update_layout(title='Deskripsi Penilaian', xaxis_title='rating', yaxis_title='Jumlah')
# Konversi grafik ke HTML
graph_html_type = fig_type.to_html(full_html=False)
graph_html_bodypart = fig_bodypart.to_html(full_html=False)
graph_html_equipment = fig_equipment.to_html(full_html=False)
graph_html_level = fig_level.to_html(full_html=False)
graph_html_rating = fig_rating.to_html(full_html=False)
return render_template('graph.html', graph_html_type=graph_html_type, graph_html_bodypart=graph_html_bodypart, graph_html_equipment=graph_html_equipment, graph_html_level=graph_html_level, graph_html_rating=graph_html_rating)
# Rute untuk menambah data baru
@app.route('/add', methods=['POST'])
def add():
new_data = {
'number': request.form['number'],
'title': request.form['title'],
'desc': request.form['desc'],
'type': request.form['type'],
'bodypart': request.form['bodypart'],
'equipment': request.form['equipment'],
'level': request.form['level'],
'rating': request.form['rating'],
'ratingdesc': request.form['ratingdesc'],
}
koleksi.insert_one(new_data)
return redirect('/')
# Rute untuk menghapus data
@app.route('/delete/<id>', methods=['GET'])
def delete(id):
koleksi.delete_one({'_id': ObjectId(id)})
return redirect('/')
# Rute untuk menampilkan form edit
@app.route('/edit/<id>', methods=['GET'])
def edit(id):
data = koleksi.find_one({'_id': ObjectId(id)})
return render_template('edit.html', data=data)
# Rute untuk menyimpan perubahan dari form edit
@app.route('/update/<id>', methods=['POST'])
def update(id):
updated_data = {
'number': request.form['number'],
'title': request.form['title'],
'desc': request.form['desc'],
'type': request.form['type'],
'bodypart': request.form['bodypart'],
'equipment': request.form['equipment'],
'level': request.form['level'],
'rating': request.form['rating'],
'ratingdesc': request.form['ratingdesc'],
}
koleksi.update_one({'_id': ObjectId(id)}, {'$set': updated_data})
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)
3. Untuk menampilkan grafik, buat Buatlah file baru untuk menyimpan script html, terdapat 3 file yaitu index.html untuk mengatur tampilan halaman awal, edit.html untuk mengatur tampilan halaman edit, dan graph.html untuk mengatur tampilan halaman grafik.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DATA GYM</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid black;
padding: 8px;
text-align: left;
max-width: 200px; /* Atur lebar maksimum untuk kolom */
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
margin: 4px; /* Menambahkan jarak pada setiap sisi */
}
th:nth-child(1),
td:nth-child(1) {
width: 5%; /* Atur lebar kolom user_id di sini */
}
th:nth-child(2),
td:nth-child(2) {
width: 8%; /* Atur lebar kolom subscription_type di sini */
}
th:nth-child(3),
td:nth-child(3) {
width: 8%; /* Atur lebar kolom monthly_revenue di sini */
}
th:nth-child(4),
td:nth-child(4) {
width: 8%; /* Atur lebar kolom join_date di sini */
}
th:nth-child(5),
td:nth-child(5) {
width: 8%; /* Atur lebar kolom last_payment_date di sini */
}
th:nth-child(6),
td:nth-child(6) {
width: 8%; /* Atur lebar kolom country di sini */
}
th:nth-child(7),
td:nth-child(7) {
width: 5%; /* Atur lebar kolom age di sini */
}
th:nth-child(8),
td:nth-child(8) {
width: 8%; /* Atur lebar kolom gender di sini */
}
th:nth-child(9),
td:nth-child(9) {
width: 8%; /* Atur lebar kolom device di sini */
}
th:nth-child(10),
td:nth-child(10) {
width: 12%; /* Atur lebar kolom plan_duration di sini */
}
/* Tambahkan margin-bottom untuk memberikan ruang antara form dan tabel */
form {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Data GYM</h1>
<p>Data Length: {{ data|length }}</p>
<button
type="button"
onclick="window.location.href='/graph'"
style="width: 150px; height: 25px; margin-top: 10px; margin-bottom: 10px"
>
Tampilkan Grafik
</button>
<form action="/" method="POST">
<div>
<label for="search">Cari berdasarkan kategori:</label>
<input type="text" id="search" name="query" />
<button type="submit"
style="width: 150px; height: 25px; border: 2px solid #000000; margin-top: 15px">Cari</button>
</div>
</form>
<style>
form input {
border: 1px solid #000000; /* Ganti dengan warna yang diinginkan */
border-radius: 2px;
padding: 8px;
margin-top: 5px;
width: 100%;
}
form {
display: flex;
flex-wrap: wrap;
}
div {
flex: 0 0 20%; /* Setiap div akan menempati 30% dari lebar container */
margin-right: 5%; /* Menambahkan jarak antara setiap div */
margin-bottom: 15px; /* Menambahkan jarak antar baris */
}
</style>
<form action="/add" method="POST">
<div><label for="number">Number:</label>
<input type="number" id="number" name="number" /></div>
<div><label for="title">Title:</label>
<input type="text" id="title" name="title" /></div>
<div><label for="desc">Description:</label>
<input type="text" id="desc" name="desc" /></div>
<div><label for="type">Type:</label>
<input type="text" id="type" name="type" /></div>
<div><label for="bodypart">Body Part:</label>
<input type="text" id="bodypart" name="bodypart" /></div>
<div><label for="equipment">Equipment:</label>
<input type="text" id="equipment" name="equipment" /></div>
<div><label for="level">Level:</label>
<input type="text" id="level" name="level" /></div>
<div><label for="rating">Rating:</label>
<input type="text" id="rating" name="rating" /></div>
<div><label for="ratingdesc">RatingDesc:</label>
<input type="text" id="ratingdesc" name="ratingdesc" /></div>
<button type="submit"
style="width: 150px; height: 35px; border: 2px solid #000000; margin-top: 20px">Tambah Data</button>
</form>
<table>
<thead>
<tr>
<th>Number</th>
<th>Title</th>
<th>Description</th>
<th>Type</th>
<th>BodyPart</th>
<th>Equipment</th>
<th>Level</th>
<th>Rating</th>
<th>RatingDesc</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{ row.number }}</td>
<td>{{ row.title }}</td>
<td>{{ row.desc }}</td>
<td>{{ row.type }}</td>
<td>{{ row.bodypart }}</td>
<td>{{ row.equipment }}</td>
<td>{{ row.level }}</td>
<td>{{ row.rating }}</td>
<td>{{ row.ratingdesc }}</td>
<td>
<form action="/edit/{{ row._id }}" method="GET">
<button type="submit">Edit Data</button>
</form>
<form action="/delete/{{ row._id }}" method="GET">
<button type="submit">Hapus</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Comments
Post a Comment