-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
76 lines (55 loc) · 1.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import dash
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
import os
import requests
from dash import dcc
from dash import html
app = dash.Dash(external_stylesheets=[dbc.themes.JOURNAL])
server = app.server
search_url = "https://api.twitter.com/2/tweets/counts/recent"
# Update to include own handle
query_params = {"query": "from:jessicagarson", "granularity": "day"}
bearer_token = os.environ.get("BEARER_TOKEN")
def bearer_oauth(r):
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "GettingStartedDash"
return r
def connect_to_endpoint(url, tweet_fields):
response = requests.request("GET", url, auth=bearer_oauth, params=tweet_fields)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
response.status_code, response.text
)
)
return response.json()
json_response = connect_to_endpoint(search_url, query_params)
df = pd.DataFrame(json_response["data"])
df["start"] = pd.to_datetime(df["start"])
final = df[["start", "tweet_count"]]
fig = px.line(final, x="start", y="tweet_count")
colors = {"background": "#FFFFFF", "text": "#1DA1F2"}
fig.update_layout(
plot_bgcolor=colors["background"],
paper_bgcolor=colors["background"],
font_color=colors["text"],
)
app.layout = html.Div(
style={"backgroundColor": colors["background"]},
children=[
html.H1(
children="Tweets by Date",
style={"textAlign": "center", "color": colors["text"]},
),
html.Div(
children="An example using Dash and the Twitter API recent search counts to see how much I've been Tweeting this week",
style={"textAlign": "center", "color": colors["text"]},
),
dcc.Graph(id="example-twitter", figure=fig),
],
)
if __name__ == "__main__":
app.run_server()