@dull is right -- you essentially need to do stuff in JavaScript that works by talking to specific URLs on your website that are designed to be accessed that way rather than by people submitting forms and so on.
To take an example you can look at -- on the "Consoles" page inside PythonAnywhere, at the bottom of the page, there is a section where you can look at the processes that are running. When you first look at it, there's a button that says "Fetch process list". If you click it, the page does not reload -- instead, after a brief pause, the bottom of the page changes to show a list of running processes. The list has a "refresh" button at the top; if you click that, again, the page does not reload, but the list is refreshed in-place. And each process has a "kill" button, which will kill the process in question, once more without loading the page.
The way that this all works -- to take the initial "Fetch process list" click as an example -- is that there is some JavaScript code that is executed when the button is clicked. This code makes an AJAX request to a URL on our site that is designed to be accessed from JavaScript, and returns the list of running processes in JSON format. The JS code receives that response, and uses it to modify the page in-place. The other buttons work similarly.
The specific libraries that we are using to do all that is the React JavaScript framework to handle all of the in-place changes to the web page from JavaScript, the Fetch API to make the Ajax requests, and Django Rest Framework to allow us to write Django code that provides an API that JavaScript can easily interact with.
But you don't need all of that if you just want to experiment; a good set of tool to start with is jQuery to do in-place modifications of the web page and also to provide a nice way to make AJAX requests. For the server side, @dull's suggestion of using flask.jsonify
is a good one when you're using Flask -- it's a simple way of converting the data that you want to send back to your JS code into a format that it can easily interpret.