如何在Django视图中返回HTML?

时间:2019-01-10 00:35:46

标签: python html django matlab-figure

我正在尝试在浏览器上显示Matlib图,我正在使用Django。到目前为止,我有:

import matplotlib.pyplot as plt
import base64
from io import BytesIO

fig = plt.figure()
#plot sth

tmpfile = BytesIO()
fig.savefig(tmpfile, format='png')
encoded = base64.b64encode(tmpfile.getvalue())

html = 'Some html head' + '<img src=\'data:image/png;base64,{}\'>'.format(encoded) + 'Some more html'

with open('test.html','w') as f:
    f.write(html)

但是,我想要的不是f.write(html),而是类似的东西:

return render_to_response(html)

我该如何使用较大的html字符串?

2 个答案:

答案 0 :(得分:0)

我是通过以下库得到的:https://mpld3.github.io/quickstart.html

fig = image_compare.get_images_figure()
html = mpld3.fig_to_html(fig)

return render(request, 'products/blank.html', {'html' : html})

和blank.html看起来像这样:

{% autoescape off %}{{ html }}{% endautoescape %}

答案 1 :(得分:0)

您尝试过HttpResponse吗?

Caused by javax.crypto.AEADBadTagException
   at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:517)
   at javax.crypto.Cipher.doFinal(Cipher.java:1736)
   at com.ca.mas.core.util.KeyUtilsSymmetric.decrypt(SourceFile:584)
   at com.ca.mas.core.security.DefaultEncryptionProvider.decrypt(SourceFile:73)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:102)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:92)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:227)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:218)
   at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
   at android.os.AsyncTask$2.call(AsyncTask.java:345)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
   at java.lang.Thread.run(Thread.java:784)


Caused by java.lang.RuntimeException
   at com.ca.mas.core.util.KeyUtilsSymmetric.decrypt(SourceFile:589)
   at com.ca.mas.core.security.DefaultEncryptionProvider.decrypt(SourceFile:73)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:102)
   at com.ca.mas.storage.MASSecureLocalStorage$2.call(SourceFile:92)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:227)
   at com.ca.mas.storage.MASSecureLocalStorage$7.loadInBackground(SourceFile:218)
   at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
   at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
   at android.os.AsyncTask$2.call(AsyncTask.java:345)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
   at java.lang.Thread.run(Thread.java:784)