python - 如何在 django 中显示 python 脚本的结果(数据框)
问题描述
class TweetAnalyzer():
""""
Class for tweet analysis
"""
def tweet_to_data_frame(self, tweets):
df = pd.DataFrame(data=[tweet.text for tweet in tweets ], columns=['tweets'])
df['id']= np.array([tweet.id for tweet in tweets])
df['len'] = np.array([len(tweet.text) for tweet in tweets])
return df
#this is the python script class
视图.py
from Filmatory.tweetstream import TweetAnalyzer
def tweetsPageView(request):
object = TweetAnalyzer()
x = object.tweet_to_data_frame('pandas.txt')
return render(request,'tweets.html',{'x':x.to_html})
解决方案
在您的模板中,您可以使用:
{{ x | safe }}
或者
{% autoescape off %}
{{ x }}
{% endautoescape %}
在模板中呈现 HTML 而无需 Django 对其进行清理。
推荐阅读
- angular - 角度动画错开动态延迟
- css - Rails - 在 application.rb 中添加了 4 个自定义资产路径,无需重新启动
- java - 处理 Objects.requireNonNull() 抛出的 NullPointerException 的最佳方法是什么?
- sql - 数据集 SQL Server 中不同 ID 的计数
- python - 从大熊猫数据框中选择条目很慢
- spring-boot - JSON 正文超出最大文档大小
- sql - 进行内部查询以删除重复的代码
- python - Word_tokenize,如何只打印第一个令牌
- c# - 使用 aspose 创建包含邮件合并数据的 Word 文档
- c - C 中 SSHD 的自定义 PAM