File size: 4,838 Bytes
8cb8290 6c7f4e8 8cb8290 6c7f4e8 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
<html>
<head>
<title>KTH Q&A</title>
<link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
<style>
@import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";
body {
font-family: 'Poppins', sans-serif;
background: #fafafa;
display: flex;
flex-direction: column;
align-items: center;
margin-left: 20%;
margin-right: 20%;
}
* {
font-family: 'Poppins', sans-serif;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
justify-items: center;
margin-top: 5%;
margin-bottom: 5%;
width: 100%;
}
h1 {
margin-top: 0;
font-size: 3em;
text-align: center;
color: blue;
margin-bottom: auto;
}
h2 {
font-size: 1em;
text-align: center;
color: grey;
margin-bottom: 2em;
}
form {
display: flex;
flex-direction: row;
margin-top: auto;
font-family: 'Poppins', sans-serif;
}
button {
margin: 0.5em;
padding: 0.5em;
background-color: blue;
color: white;
border: none;
border-radius: 0.5em;
padding-left: 1em;
padding-right: 1em;
font-size: 1.1em;
font-weight: 300;
line-height: 1.7em;
transition: all 0.3s;
cursor: pointer;
align-self: self-end;
}
button:disabled {
background-color: #ccc;
color: #666;
cursor: not-allowed;
}
button:hover {
background-color: #ace;
color: #fff;
}
input {
margin: 0.5em;
padding: 0.5em;
width: 35em;
}
p {
font-size: 1.1em;
font-weight: 300;
line-height: 1.7em;
color: #999;
max-width: 30em;
}
a {
color: blue;
text-decoration: none;
transition: all 0.3s;
}
a:hover,
a:focus {
color: cornflowerblue;
text-decoration: none;
transition: all 0.3s;
}
#content {
width: 100%;
padding: 20px;
min-height: 100vh;
transition: all 0.3s;
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<div class="container">
<h1>KTH Q&A</h1>
<h2>Your digital Study Counsellor</h2>
<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> |