Spaces:
Sleeping

File size: 1,994 Bytes
c684ee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Checkbox List Flask App with Similarity Check</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <input type="text" id="inputItem"/>
    <input type="text" id="similarityResult" readonly />
    <button onclick="addItem()">Send</button>
    <button onclick="listItems()">Check</button>
    <button onclick="checkSimilarity()">Similarity</button>
    <div id="checkboxList" style="display:none;"></div>
    
    <script>
        function addItem() {
            const item = $('#inputItem').val();
            $.ajax({
                url: '/add_item',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ 'item': item }),
                success: function(response) {
                    if (response.success) {
                        alert(`Added: ${response.item}`);
                        $('#inputItem').val(''); // Clear the input after adding the item
                    } else {
                        alert('Failed to add item.');
                    }
                }
            });
        }

        function listItems() {
            // Only toggles visibility here, does not clear or repopulate the list
            $('#checkboxList').toggle();
        }

        function checkSimilarity() {
            const item = $('#inputItem').val();
            $.ajax({
                url: '/check_similarity',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ 'item': item }),
                success: function(response) {
                    if (response.similar) {
                        $('#similarityResult').val('Yes!');
                    } else {
                        $('#similarityResult').val('No match found.');
                    }
                }
            });
        }
    </script>
</body>
</html>