Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Last active August 29, 2015 14:17
Show Gist options
  • Save chriddyp/6962d2cdf8c2da4a9b71 to your computer and use it in GitHub Desktop.
Save chriddyp/6962d2cdf8c2da4a9b71 to your computer and use it in GitHub Desktop.
def files_list(request):
import requests
import json
from requests.auth import HTTPBasicAuth
from django.shortcuts import redirect, render
auth = HTTPBasicAuth('chris', '')
headers = {'Plotly-Client-Platform': 'api', 'Content-Type': 'application/json'}
r = requests.get('https://api.plot.ly/v2/files/chris:-1', headers=headers, auth=auth)
files = json.loads(r.content)['results']
for f in files:
if f['filetype'] == 'plot':
f['url'] = 'https://plot.ly/~' + '/'.join(f['fid'].split(':'))
else:
files.remove(f)
return render(request, 'listview.html', {'items': files})
{% block content %}
{% for i in items %}
<div>
{{i.filename}}
<a href="{{i.url}}">
<img src = "{{i.url}}.png">
</a>
</div>
{% endfor %}
{% endblock %}
@chriddyp
Copy link
Author

For interactive graphs, use:

{% block content %}
    {% for i in items %}
    <div>
        {{i.filename}}
        <div>
            <a href="{{i.url}}" target="_blank" title="Plot Title" style="display: block; text-align: center;">
                <img src="{{i.url}}.png" alt="Plot Title" style="max-width: 100%;width: 500px;"  width="500" onerror="this.onerror=null;this.src='https://plot.ly/404.png';" />
            </a>
            <script data-plotly="{{i.fid}}" src="https://plot.ly/embed.js" async>
            </script>
        </div>
    </div>
    {% endfor %}
{% endblock %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment