Skip to content

Commit

Permalink
Compare works someone now
Browse files Browse the repository at this point in the history
  • Loading branch information
Bender authored and Bender committed Apr 29, 2024
1 parent 09568bc commit 0820a27
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
70 changes: 70 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,78 @@ main {
border: none;
box-shadow: 0 4px 14px 0 rgba(175, 238, 162, 0.39);
}
#compared-cart {
display: none;
}

.compared-item {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
flex-wrap: wrap;
padding: 20px;
width: 50%;
margin: 0 auto;
background-color: var(--bgcolortrans);
backdrop-filter: blur(10px);
box-shadow: var(--bgcolor) 0px 5px 5px -5px;
border-radius: 10px;
gap: 20px; /* Add this line to create a gap between items */
}
.compared-item-row {
display: flex;
justify-content: center;
margin-bottom: 20px;
}

.compared-item {
margin: 0 10px;
text-align: center;
}

.compared-item img {
display: block;
max-width: 100%;
height: auto;
}
.compared-item-row {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.compared-item {
background-color: rgba(255, 0, 0, 0.2); /* Red background for compared item */
}

.original-item {
background-color: rgba(0, 128, 0, 0.2); /* Green background for original item */
}

.compared-item p {
margin-top: 10px;
}
.addtocart:hover {
background-color: var(--accentbg);
box-shadow: 0 6px 20px rgba(24, 214, 81, 0.23);
}

.store-dropdown {
font-family: 'Inter', sans-serif;
font-weight: bold;
padding: 15px;
font-size: 15px;
border-radius: 5px;
border: none;
width: auto;
background-color: var(--accentcolor);
color: var(--textcolor);
box-shadow: 0 4px 14px 0 rgba(175, 238, 162, 0.39);
transition: background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.store-dropdown:hover {
box-shadow: 0 6px 20px rgba(24, 214, 81, 0.23);
background-color: var(--accentlight);
color: var(--accentcolor);
}
41 changes: 38 additions & 3 deletions templates/cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2>Food Cart</h2>

</div>
{% if foodcart|length == 0 %}
<div class="cart">
<div class="cart" id="current-cart">
<h2>Your cart is empty</h2>
<p>Go back to the main page to add items to your cart</p>
</div>
Expand All @@ -47,11 +47,46 @@ <h2>The Total Cost Of This Cart is </h2>
</form>
<!-- Add a compare dropdown -->
<div class="compare-dropdown">
<button class="compare-dropdown-button">Compare with similar carts</button>
<button class="cartbtn" id="compare-button">Compare with similar carts</button>
</div>
<!-- Display the estimated monthly grocery list -->
<div id="monthly-estimate"></div>


<div id="compared-cart"></div>
<script>
$('#compare-button').click(function() {
$.ajax({
url: '/compare_carts',
type: 'GET',
success: function(data) {
var similarity_scores = data.similarity_scores;
var compared_cart_html = '';
for (var i = 0; i < similarity_scores.length; i++) {
var current_item = similarity_scores[i][0];
var other_item = similarity_scores[i][1];
var similarity_score = similarity_scores[i][2];
compared_cart_html += '<div class="compared-item-row">';
compared_cart_html += '<div class="compared-item original-item">';
compared_cart_html += '<img src="' + current_item.image_url + '" alt="' + current_item.item_name + '">';
compared_cart_html += '<p>' + current_item.item_name + ' - $' + current_item.price + '</p>';
compared_cart_html += '</div>';
if (other_item !== null) {
compared_cart_html += '<div class="compared-item">';
compared_cart_html += '<img src="' + other_item.image_url + '" alt="' + other_item.item_name + '">';
compared_cart_html += '<p>' + other_item.item_name + ' - $' + other_item.price + '</p>';
compared_cart_html += '<p>Similarity Score: ' + similarity_score + '</p>';
compared_cart_html += '</div>';
}
compared_cart_html += '</div>';
}
$('#compared-cart').html(compared_cart_html);
$('#current-cart').hide();
$('#compared-cart').show();
}
});
});
</script>
</script>
<script>
$.ajax({
url: '/get_total_cost',
Expand Down
21 changes: 12 additions & 9 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,21 @@ def compare_carts():
other_cart = read_csv(csvfile)
print(f"Other cart: {other_cart}")

matched_items = set()
for other_item in other_cart:
other_item_key = (other_item['item_name'], other_item['store'])
for current_item_key in current_cart_set:
similarity_score = fuzz.partial_token_sort_ratio(other_item_key, current_item_key)
if similarity_score > 75: # adjust the threshold as needed
# Find the current_item that matches current_item_key
current_item = next((item for item in current_cart if (item['item_name'], item['store']) == current_item_key), None)
if current_item is not None:
similarity_score = process.extractOne(current_item['item_name'], [other_item['item_name']], scorer=fuzz.partial_token_sort_ratio)[1]
if similarity_score is not None: # Only append if the score is not None
similarity_scores.append((current_item, other_item, similarity_score))

if current_item_key not in matched_items:
similarity_score = fuzz.partial_token_sort_ratio(other_item_key, current_item_key)
if similarity_score > 75: # adjust the threshold as needed
# Find the current_item that matches current_item_key
current_item = next((item for item in current_cart if (item['item_name'], item['store']) == current_item_key), None)
if current_item is not None:
similarity_score = process.extractOne(current_item['item_name'], [other_item['item_name']], scorer=fuzz.partial_token_sort_ratio)[1]
if similarity_score is not None: # Only append if the score is not None
similarity_scores.append((current_item, other_item, similarity_score))
matched_items.add(current_item_key) # Add the current_item_key to the matched_items set

print(f"Similarity scores: {similarity_scores}")

# Sort the similarity scores in descending order
Expand Down

0 comments on commit 0820a27

Please sign in to comment.