Forums

Loading dynamically changing html files using flask

I've been trying to build a flask application which uses dynamic html files. My application uses the flask_app2.py to call two functions in parser.py and transitions.py. The application basically presents a branching story based on an option presented in each page. Which page should be loaded next is sent as a POST request which is then passed to parser.py and transitions.py to generate a a new html file. I'm using a single html file page.html and which keeps changing based on the option selected. However only a static version of this file is loaded when the reload button is pressed on the application page on the pythonanywhere site which results in the same page being shown no matter the button being pressed. I was wondering if anyone could tell me if there's something I could do so that the template html file dynamically changes? Below is the method in my flask file used to generate the new file. make_option_files and make_file are imported from other python files and used to change the contents of page.html.

def next():
if request.method == 'POST':
    form = request.form['option']
    # Page for the pronunciation evaluation.
    if(form == "PronEval"):
        return render_template('PronEval.html')
    # Page for inteligibility remediation information.
    elif(form == "Info"):
        return render_template('Info.html')
    else:
        make_option_files(form)
        make_file(form)
        return render_template('page.html')

The structure of my directory is given below.

├─ AROWF-recently.txt                                                                        ├── __init__.py                                 
├── _config.yml                                                                              ├── cmudict-en-us.dict                           
├── flask_app2.py
├── initial.js
├── jsgf_file_parser.py
├── parser.py
├── parser.pyc
├── sphinx4-5prealpha-src.zip
├── static
│   ├── css
│   │   └── materialize.min.css
│   └── js
│       ├── audioRecorder.js
│       ├── audioRecorderWorker.js
│       ├── callbackManager.js
│       ├── materialize.min.js
│       ├── pocketsphinx.js
│       ├── recognizer.js
│       └── recorder.js
├── templates
│   ├── Info.html
│   ├── PronEval.html
│   ├── page-options.txt
│   ├── page.html
│   └── page.txt
├── transitions.py
└── transitions.pyc

I suspect your template for page.html is being cached after the first time it's loaded. Changing the template file dynamically probably isn't a good plan, to be honest -- if several people were visiting the site at the same time, things would get confused pretty quickly. I'd recommend using the Flask templating engine to dynamically put text into the page at runtime.

I shall try that. It would actually be better in the long run. Another problem I'm facing is I'm not able to access the web browser's microphone using pythonanywhere. Is that not possible using pythonanywhere or do I have to add something else?

You'll need to use JavaScript code to capture audio from the browser's microphone, then send it back to your server-side Python code. This introductory article explains the appropriate JavaScript API.

I've already made the recorder api which I've been using for a while. The following code snippet is what I've been using to check whether the browser supports audio,

try {
      window.AudioContext = window.AudioContext || window.webkitAudioContext;
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
      window.URL = window.URL || window.webkitURL;
      audioContext = new AudioContext();

      inSampleRate = audioContext.sampleRate;
    } catch (e) {
      updateStatus("Error initializing Web Audio browser");
    }
    if (navigator.getUserMedia) navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
                                    updateStatus("No live audio input in this browser");
                                });
    else updateStatus("No web audio support in this browser");

The "No live audio input in this browser" error is being thrown. It works fine when I run it locally but is failing on the pythonanywhere site. Any ideas regarding this?

Is there anything in the browser's developer tools console? It might be that code that's running from a local server (or from an HTML page loaded via a file) has a more permissive security context.