首页 > 解决方案 > 如何在 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})

标签: pythondjangodataframe

解决方案


在您的模板中,您可以使用:

{{ x | safe }} 

或者

{% autoescape off %}
    {{ x }}
{% endautoescape %}

在模板中呈现 HTML 而无需 Django 对其进行清理。


推荐阅读