简体   繁体   English

运行时错误:主线程不在主循环中使用带有 Django 的 Matplotlib

[英]RuntimeError: main thread is not in main loop using Matplotlib with Django

I am creating a Matplotlib figure to display in a HTML template in my Django application.我正在创建一个 Matplotlib 图以显示在我的 Django 应用程序的 HTML 模板中。 I send this figure to the HTML by saving it under my static files, and then loading an img tag with the saved .png .我将这个图保存在我的静态文件下,然后将它发送到 HTML,然后加载一个带有保存的.pngimg标签。 I do this in my views.py after getting a reference to the figure.在获得对该图的引用后,我在我的views.py执行此操作。

        # Get analysis visualization chart
        figure = analyser.visualize_tweets(emotions)

        # Save figure in static folder as png
        figure.savefig('static/analysis_figures/figure.png')

        # Inject html with figure path
        response['analysis_figure_path'] = 'analysis_figures/figure.png'

return render(request, 'landing_page/index.html', response)

My HTML is something like this:我的 HTML 是这样的:

<img src={% static analysis_figure %} alt="">

However, this caused the RuntimeError: main thread is not in main loop to happen when the function in my views.py is called a second time (if it's called once everything works properly).但是,这会导致RuntimeError: main thread is not in main loop在我的views.py的函数被第二次调用时发生(如果它在一切正常views.py调用)。 To prevent this error, I moved saving the Matplotlib figure to main() as to run in the main thread and then call it in my original function.为了防止这个错误,我将 Matplotlib 图形保存到main()以在主线程中运行,然后在我的原始函数中调用它。 This fixed the error, but prevents my HTML from reloading so every time the user submits a query the new figure is displayed over the previous one without the previous one being removed.这修复了错误,但会阻止我的 HTML 重新加载,因此每次用户提交查询时,新图形都会显示在前一个之上,而不会删除前一个。 Any ideas on any of the problems ?关于任何问题的任何想法?

I think this post explains what to do: How to clean images in Python / Django?我认为这篇文章解释了该怎么做: How to clean images in Python / Django?

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

As explained here: https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server如此处所述: https : //matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server

To prevent that the new figure is displayed over the previous one use: plt.close() / figure.close()为防止新图形显示在前一个上,请使用: plt.close() / figure.close()

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt


plt.bar(x, y, tick_label = tick_label, 
width = 0.8, color = ['red','yellow', 'green']) 
    

plt.xlabel('x - axis') 

plt.ylabel('y - axis') 

plt.title('My bar chart!') 

plt.style.use('fivethirtyeight')
    
fig=plt.gcf()
plt.close()

`enter code here`# convert graph
buf=io.BytesIO()
fig.savefig(buf,format='png')        
buf.seek(0)
string =base64.b64encode(buf.read())

uri=urllib.parse.quote(string)

context={'imgdata':uri}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib 和 :RuntimeError: 主线程不在主循环中: - Matplotlib and :RuntimeError: main thread is not in main loop: RuntimeError:主线程不在主循环中 - RuntimeError: main thread is not in main loop 使用 matplotlib 绘制图形时出现间歇性错误“RuntimeError: main thread is not in main loop” - Intermittent error while plotting graph using matplotlib “RuntimeError: main thread is not in main loop” RuntimeError:使用tkinter.simpledialog时主线程不在主循环中 - RuntimeError: main thread is not in main loop while using tkinter.simpledialog (Python tkinter):RuntimeError:主线程不在主循环中 - (Python tkinter): RuntimeError: main thread is not in main loop PySimpleGUI。 运行时错误:主线程不在主循环中 - PySimpleGUI. RuntimeError: main thread is not in main loop kochat in use RuntimeError: 主线程不在主循环中 - kochat in use RuntimeError: main thread is not in main loop RuntimeError:主线程不在主循环中-绘制两个图 - RuntimeError: main thread is not in main loop-Plotting in two figures 调试 Tkinter 代码给出“RuntimeError:主线程不在主循环中” - Debugging Tkinter code gives "RuntimeError: main thread is not in main loop" RuntimeError:在 Timer 上设置 y 坐标时,主线程不在主循环中 - RuntimeError: main thread is not in main loop when setting y coordinate on Timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM