0

I have a function which uses matplotlib and pandas to crunch tweets from txt file and plot a graph.

I'm using that script in views.py in django. It shows the output correctly the first time, however on executing the webpage second time leads to an infinite loop.

I am trying to figure out the reason but can't solve it. Here is the views.py function:

def main():
    print 'Reading Tweets\n'
    tweets_data_path = 'twitter_data.txt'
    tweets_data = []
    tweets_file = open(tweets_data_path, "r")

    for line in tweets_file:
        try:
            tweet = json.loads(line)
            tweets_data.append(tweet)
        except:
            continue

    print 'Structuring Tweets\n'
    tweets = pd.DataFrame()
    tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
    tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)

    print 'Adding programming languages tags to the data\n'

    tweets['python'] = tweets['text'].apply(
        lambda tweet: word_in_text('python', tweet)
    )
    tweets['javascript'] = tweets['text'].apply(
        lambda tweet: word_in_text('javascript', tweet)
    )
    tweets['ruby'] = tweets['text'].apply(
        lambda tweet: word_in_text('ruby', tweet)
    )

    print 'Analyzing tweets by programming language\n'
    prg_langs = ['python', 'javascript', 'ruby']

    tweets_by_prg_lang = [
        tweets['python'].value_counts()[True],
        tweets['javascript'].value_counts()[True], 
        tweets['ruby'].value_counts()[True]
    ]

    x_pos = list(range(len(prg_langs)))
    width = 0.8
    fig, ax = plt.subplots()
    plt.bar(x_pos, tweets_by_prg_lang, width, alpha=1, color='g')
    ax.set_ylabel('Number of tweets', fontsize=15)
    ax.set_title('Ranking:', fontsize=10, fontweight='bold')
    ax.set_xticks([p + 0.4 * width for p in x_pos])
    ax.set_xticklabels(prg_langs)
    plt.grid()
    plt.show()
return render('analytics.html')

This is the url which calls the main function:

 url(r'^analytics/$', 'newsletter.analytics.main', name='analytics'),

It executes in terminal as many times as i want. But stuck in webpages. Please shed some light on me !! P.S i am new to Django

1 Answers1

0

Your function needs to take the request as an arg & return a response that can be rendered in the browser.

Then you'll need to decide how to display it, so essentially you'll need to do;

def main(request, *args, **kwargs):
    # all your existing code first, then respond to the request;

    canvas = FigureCanvas(fig)
    response= HttpResponse(mimetype='image/png')
    canvas.print_png(response)
    return response

Then for IE support (some versions ignore content_type), your URL should specify the image extension;

url(r'^ analytics/simple.png$', 'newsletter. analytics.main'),

If you want it in a popup or similar, maybe you can then look at creating an ajax response. So instead of returning like that, check for if request.is_ajax: then return something like HttpResponse(json_data, mimetype="application/json").

I've just seen your edit where you've added the return render('analytics.html'). That'll just render the template you've got. You want to pass context with that so that you can display the data you've processed, or just return an image of what you've processed similar to above.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • i tried your solution to display image in a page. It's successful first time but when tryin again it fails. – Sangit Dhanani Dec 06 '15 at 12:10
  • @SangitDhanani It's not so much a solution, as a step towards what you need. If it doesn't work second time, you'll need to ask yourself why, debug it & find out what it is actually doing. – markwalker_ Dec 06 '15 at 12:14
  • Ya i did that. When i execute the second time it works upto "Analyzing tweets by ...". Then it freezes and server runs forever. – Sangit Dhanani Dec 06 '15 at 12:39
  • Great, I'd add some more print statements in that section of the code & also output the values of things to ensure that you've got the data you expect in `tweets_by_prg_lang` and `tweets` etc. You're not running any loops there so if it's not returning, hopefully you can find where it's getting stuck. – markwalker_ Dec 06 '15 at 12:44
  • im getting stuck at "fig, ax = plt.subplots()". I am looking for a solution on net. If you know then let me know. – Sangit Dhanani Dec 06 '15 at 13:17
  • @SangitDhanani Yes, you're not passing anything to `subplots()` so I suspect that'll be your problem. Check the docs; http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot – markwalker_ Dec 06 '15 at 15:14
  • 1
    I used try..except and in except i appended 0 if tweets were not found. That was the problem. Thanks it solved !! – Sangit Dhanani Dec 07 '15 at 02:19