图像在JavaScript和Python函数结束之前未加载完成。

huangapple go评论75阅读模式
英文:

Image not loading before Javascript+Python function ends

问题

以下是您的内容的中文翻译:

我正在使用Python和Eel与本地网站进行前端通信。我的代码应该在长时间运行的Python函数中在网页上显示一张图片。尽管HTML中正确的时间存在<img>标签,但图片只在Python函数执行后出现。

这是我的JavaScript代码:

eel.expose(set_image);

function set_image() {
    document.getElementById("zoom-animate").innerHTML = '<img src="temp.png">';
}

function generate() {
    let source = document.getElementById("source").value;
    let keyword = document.getElementById("keyword").value;
    eel.generate(source, keyword);
}

这是我的Python代码:

@eel.expose
def generate(source, keyword):
    # 需要长时间执行的代码 1
    eel.set_image()
    # 需要长时间执行的代码 2
英文:

I'm using python with eel to communicate with a local website as frontend. My code should display an image on a web page while in the middle of a long python function. The image only appears after the python function is executed, even though &lt;img&gt; is present in the HTML at the correct time.

Here's my Javascript code:

eel.expose(set_image);

function set_image() {
    document.getElementById(&quot;zoom-animate&quot;).innerHTML = &#39;&lt;img src=&quot;temp.png&quot;&gt;&#39;;
}



function generate() {
    let source = document.getElementById(&quot;source&quot;).value;
    let keyword = document.getElementById(&quot;keyword&quot;).value;
    eel.generate(source, keyword);
}

And my Python code:

@eel.expose
def generate(source, keyword):
	# code that takes a long time to execute 1
    eel.set_image()
	# code that takes a long time to execute 2

答案1

得分: 0

Your generate() function should call set_image() and immediately return. That way the WSGI server can immediately send a 200 document to the web client. As it stands, the web server is patiently waiting for your function to return.

Use a celery worker to complete the "long time to execute 2" task in the background.

英文:

Your generate() function should call set_image() and immediately return.
That way the
WSGI
server can immediately send a 200 document to the web client.
As it stands, the web server is patiently waiting for your function to return.

Use a celery
worker to complete the "long time to execute 2" task
in the background.

huangapple
  • 本文由 发表于 2023年5月30日 01:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定