第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Flask:在繼續(xù)功能的同時(shí)在網(wǎng)頁(yè)上顯示新生成的圖像?

Flask:在繼續(xù)功能的同時(shí)在網(wǎng)頁(yè)上顯示新生成的圖像?

炎炎設(shè)計(jì) 2021-05-30 10:01:52
我有一個(gè)網(wǎng)頁(yè),用戶在其中上傳圖片,我的 Python 代碼將對(duì)它進(jìn)行一些調(diào)整并進(jìn)行一些分析。我希望新生成的圖像在生成后立即在網(wǎng)頁(yè)上顯示給用戶,然后繼續(xù)進(jìn)行需要進(jìn)行的分析,然后使用該信息更新網(wǎng)頁(yè)。但是,我不確定如何在功能中途(一旦生成新圖像)從flask到網(wǎng)頁(yè)通信網(wǎng)站可以顯示新生成的圖像,因?yàn)槭褂胷ender_template只能在功能結(jié)束時(shí)完成。我的python(燒瓶)代碼如下:@app.route('/uploaded', methods=['GET', 'POST'])def upload_file():    if request.method == 'POST':            filename = secure_filename(file.filename)            f = request.files['file']            f.save(secure_filename(f.filename))            image = cv2.imread(filename)            im = Image.fromarray(image)            # make some adjustments to the image (not shown here) and then save it...            im.save(os.path.join(app.config['UPLOAD_FOLDER'], 'your_file.jpg'))            # after the new image is generated, display it on the website at {{ place_for_generated_image }}            # do some more analysis, then finally:            return render_template('index.html', analysis = analysis)HTML很簡(jiǎn)單:<form action = "http://localhost/uploaded" method = "POST"             enctype = "multipart/form-data">            <input type = "file" name = "file" class="form-control-file">            <input type = "submit" class="btn btn-info" value="Upload Image" button id="uploadfile" onclick="uploadfile()">            </form>            {{ place_for_generated_image }}            {{ analysis }}
查看完整描述

1 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

您需要使用多個(gè) ajax 調(diào)用來(lái)實(shí)現(xiàn)這一點(diǎn)。類似于以下內(nèi)容:


首先制定處理圖像上傳的途徑


@app.route('/uploaded', methods=['GET', 'POST'])

def upload_file():

    if request.method == 'POST':

            filename = secure_filename(file.filename)

            f = request.files['file']

            f.save(secure_filename(f.filename))

            image = cv2.imread(filename)

            im = Image.fromarray(image)

            # make some adjustments to the image (not shown here) and then save it...

            im.save(os.path.join(app.config['UPLOAD_FOLDER'], 'your_file.jpg'))

            # Here you need to convert your image to a base64 string and return that string to your ajax call

            # do some more analysis, then finally:

            return 'YOUR BASE64 ENCODED IMAGE STRING'

這條路線將處理您的分析。您的第二個(gè)嵌套 ajax 調(diào)用將與此路由從flask import Response 進(jìn)行通信


@app.route('/analyze', methods=['GET', 'POST'])

    def analyze():

        # run some analysis

        return Response(status=200, response="Anlysis Done")

這就是您的 javascript 代碼的樣子。您可以將其放置在模板中的腳本標(biāo)記中。如果你把它放在一個(gè)單獨(dú)的 JS 文件中,一定要看看我在第二個(gè) ajax 調(diào)用中對(duì) url_for 的評(píng)論


$('form').submit(function(e){

    e.preventDefault();

    $.ajax({

        url: $(this).attr('action'),

        type: 'POST',

        data: $(this).serialize()

    }).done(function(res){

        // Here you write code that will display the returned base64 image 

        // from the upload_file route Just update your img tag src to the 

        // base64 string you received as the response


        // SECOND AJAX CALL TO RUN THE ANALYTICS 

        $.ajax({

            url: "{{url_for('analysis')}}", // if this JS code in a separate JS file you will have to declare a URL variable in a script tag in your template as {{url_for}} is only accessible from your template 

            type: 'POST',

            data: 'any data you want to send can be in JSON format' 

        }).done(function(res){

            // analysis is done 

        })

    })

})


查看完整回答
反對(duì) 回復(fù) 2021-06-01
  • 1 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)