javascript is not working with flask applications

65 views Asked by At

i have got data from the app.py written in flask. i have logged the data in script section and data was correct but when i use to create an tag or update the src of an existing tag, the tags are being displayed empty.

i am using javascript to give action to the webpage. when i click a button a video present in the static folder will be displayed and played. my flask method :-

@app.route('/get_video_url')
def get_video_url():
    video_path = "static/output.mp4"
    if not os.path.exists(video_path):
        print("not found")
        return jsonify({'video_url': "no"})
    return jsonify({'video_url': "output.mp4"})

my file Structure:- File structure

my javascript:-

document.getElementById('playButton').addEventListener('click', async () => {
            try {
                const response = await fetch('/get_video_url');
                const data = await response.json();
                console.log(data)
                const videoUrl = data.video_url;
                console.log(videoUrl)
                // Set the video source
                if (videoUrl === "no") {
                    document.getElementById('myVideo').innerHTML = `<p>video not found</p>`;
                } else {
                    document.getElementById('myVideo').src = "/static/output.mp4";
                    document.getElementById('myVideo').style.display = 'block';
                }
            } catch (error) {
                console.error('Error fetching video URL:', error);
            }
        });

what i can see in page source:- document.getElementById('myVideo').src = "/static/output.mp4"; but my video was blank. The server was sending the responses correctly. but javascript was unable to update the responses. my terminal:-

127.0.0.1 - - [19/Feb/2024 21:51:42] "GET /static/output.mp4 HTTP/1.1" 206 -
1

There are 1 answers

0
siddardha annapureddy On

Actually the javascript was working, the problem lies in the video i am trying to render. The video codec was not matched with the browser and video was black and empty. i just replaced fourcc as -1 and problem solved in opencv.