I am using the code below to show a user's Twitter profile on the page. In the example below I'm trying to show the profile for the username 'dogculture'. Why isn't this working?
__init__.py
def get_user_profile(twitter_api, screen_names=None, user_ids=None):
items_to_info = {}
items = screen_names or user_ids
while len(items) > 0:
# Process 100 items at a time per the API specifications for /users/lookup.
items_str = ','.join([str(item) for item in items[:100]])
items = items[100:]
if screen_names:
response = make_twitter_request(twitter_api.users.lookup, screen_name=items_str)
else: # user_ids
response = make_twitter_request(twitter_api.users.lookup, user_id=items_str)
for user_info in response:
if screen_names:
items_to_info[user_info['screen_name']] = user_info
else: # user_ids
items_to_info[user_info['id']] = user_info
return items_to_info
profile.html
{% block body %}
<body>
<div class="container">
twitter_api = oauth_login()
response = make_twitter_request(twitter_api.users.lookup, screen_name="dogculture")
print json.dumps(response, indent=1)
</div>
</body>
{% endblock %}
You need to write a view to get the Twitter data and render the template. You would pass the profile data to the template, not write Python code in it.
@app.route('/profile/<username>')
def profile(username):
twitter_api = oauth_login()
profiles = get_user_profile(twitter_api, screen_names=(username,))
profile = profiles.get(username)
if profile is None:
abort(404)
return render_template('profile.html', profile=profile)
{% block body %}
Render the contents of the profile dict here.
For example, here's the username.
{{ profile['username'] }}
{% endblock %}