Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce plots in tweets #1

Open
fuglede opened this issue Apr 2, 2020 · 10 comments
Open

Introduce plots in tweets #1

fuglede opened this issue Apr 2, 2020 · 10 comments

Comments

@fuglede
Copy link
Contributor

fuglede commented Apr 2, 2020

It's relatively straightforward to include images in Twitter updates. In our case, it would make sense to include a small image showing the current forecast.

@Alig1493
Copy link

Alig1493 commented Oct 1, 2021

When you say small image are you referring to the green and black icons that are being returned in the json from the api? Or will it be from another known source?

@fuglede
Copy link
Contributor Author

fuglede commented Oct 2, 2021

I was actually thinking something more like the plot of the forecast that's currently on the web page:

image

The plot is currently rendered client-side, but it should be possible to render it when generating the tweets as well.

@Alig1493
Copy link

Alig1493 commented Oct 2, 2021

I saw that the plot of the referred forecast above is rendered by the front end using the plot-data. I am not being able to use that plot-data in the response to render a graph using altair as you had previously mentioned. My initial efforts were as follows:

import altair
import json
import requests

url = "https://xn--grnstrm-r1ae.nu/api/v1/current-emission-intensity"
response = requests.get(url)
jd = json.dumps(response.json().get("plot-data"))
altair.Chart.from_json(jd)

Shortened Traceback:
AttributeError: 'list' object has no attribute 'get'

I may have made mistakes in my approach and I'd appreciate if you'd help me navigate them. Thanks for your comments thus far.

@fuglede
Copy link
Contributor Author

fuglede commented Oct 2, 2021

In your traceback, which of the two get is it complaining about? Your snippet doesn't define requests; perhaps you accidentally replaced the imported module with a list, or response has somehow become a list? For what it's worth, if I import requests before calling your snippet, it does work as expected.

@Alig1493
Copy link

Alig1493 commented Oct 2, 2021

I forgot to paste the requests in the snippet. Did it really work because I tried it again after your comment and I'm still getting the same error for the plot-data in the response_data for the url given.

@fuglede
Copy link
Contributor Author

fuglede commented Oct 2, 2021

Yep, here's with I get:

$ cat test.py
import altair
import json
import requests

print(altair.__version__)
print(requests.__version__)
url = "https://xn--grnstrm-r1ae.nu/api/v1/current-emission-intensity"
response = requests.get(url)
jd = json.dumps(response.json().get("plot-data"))
print(altair.Chart.from_json(jd))


$ python test.py
4.1.0
2.26.0
alt.VConcatChart(...)

@Alig1493
Copy link

Alig1493 commented Oct 3, 2021

4.1.0
2.26.0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

This is my output unfortunately for the same snippet above. What version for python are you using?

@fuglede
Copy link
Contributor Author

fuglede commented Oct 3, 2021

What is the value of response.json() in your case?

@Alig1493
Copy link

Alig1493 commented Oct 3, 2021

plot_data_response.txt

@fuglede
Copy link
Contributor Author

fuglede commented Oct 3, 2021

That looks about right as well; as in,

altair.Chart.from_json(json.dumps(requests.get("https://github.com/groenstroem/twitter/files/7273244/plot_data_response.txt").json().get('plot-data')))

does what it should. You'll have to either get the full traceback, or debug each step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants