File size: 2,139 Bytes
8cb8290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
<html>

<head>
    <title>KTH Q&A</title>
    <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
</head>

<body>
    <div class="container">
        <h1>KTH Q&A</h1>
        <form name="form" method="post">
            <input type="text" name="question" />
            <button type="submit" id="ask">Ask</button>
        </form>
        <p id="answer"></p>
        <p id="readmore"></p>
        <ul id="urls"></ul>
    </div>
    <script>
        const form = document.querySelector('form');
        form.addEventListener('submit', async (event) => {
            document.getElementById('ask').disabled = true;
            event.preventDefault();
            const formData = new FormData(form);
            const question = formData.get('question');
            const response = await fetch('/api/ask', {
                method: 'POST',
                body: JSON.stringify({ question }),
                headers: {
                    'content-type': 'application/json'
                }
            });
            const data = await response.json();
            console.log(data);
            document.querySelector('#answer').textContent = data.answer;
            if (data.urls && data.urls.length > 0) {
                document.getElementById('readmore').textContent = 'You might find related info at: ';
                const urls = document.getElementById('urls');
                urls.innerHTML = '';
                data.urls.forEach(url => {
                    const li = document.createElement('li');
                    const a = document.createElement('a');
                    a.href = url;
                    a.textContent = url;
                    li.appendChild(a);
                    urls.appendChild(li);
                });
            } else {
                document.getElementById('readmore').textContent = '';
                document.getElementById('urls').innerHTML = '';
            }
            document.getElementById('ask').disabled = false;
        });
    </script>
    {% if DEBUG %}
    {{ hotreload.script(url_for('hot-reload')) | safe }}
    {% endif %}
</body>

</html>