For this particular application, it would be more suitable to do the download in the browser using javascript only. Because you are not trying to modify the text using data stored on your server, your server backend application (Python), does not need to know that the user is downloading the file. This is good because it will reduce load on your server and the user will be able to perform the download even if they temporarily lose internet connection.
Unfortunately for you, but fortunately for the security of the user, you don't get to decide the location at which to download the file onto the user's computer, so you can't make it save to their desktop. Typically it will go into their downloads folder. You can, however, set the filename to whatever you want.
The following solution is mostly taken from @MatějPokorný's answer here and should work for you.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function download(filename, text) {
/*
code from StackOverflow user Matěj Pokorný at
https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server#annswer-18197341
*/
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>
<form onsubmit="download('webpage_entry.txt', this['string'].value)" method="POST">
<input
type="text"
value="String"
name="string"
/>
</form>
</body>
</html>
If you for whatever reason did want to involve the backend in the download, you could set up a handler on your server which receives POST requests to route /a
and responds with file data.