27
loading...
This website collects cookies to deliver better user experience
pip3 install dash
pip3 install dash-bootstrap-components
pip3 install requests
pip3 install pandas
app.py
file. In your terminal, you will want to make a python file entitled app.py
.touch app.py
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import os
import pandas as pd
import requests
app
. In this variable, you are initializing the app you are making and pulling in the bootstrap theme of journal. Using a bootstrap theme is just one way that you can start customizing your page.app = dash.Dash(external_stylesheets=[dbc.themes.JOURNAL])
server = app.server
search_url
.search_url = "https://api.twitter.com/2/tweets/counts/recent"
query_params
to define the query and granularity of the counts you’ll get back. In the parameter entitled query, you can specify the rules of what will get returned from the Twitter API. In the example below, the query is set to be all Tweets from the handle @jessicagarson
. You can swap this out for your handle if you wish. To learn more about creating a query, check out our documentation on the subject.query_params = {'query': 'from:jessicagarson','granularity': 'day'}
your-bearer-token
with your bearer token. Be sure to keep the single quotes around your Bearer Token. You can get your bearer token from the keys and tokens section of your App inside the developer portal.export BEARER_TOKEN='your-bearer-token'
bearer_token
in your code editor, and use the os module to get the bearer_token
environment variable set in the terminal.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'])
to_datetime
method in pandas.df['start'] = pd.to_datetime(df['start'])
tweet_count
columns, you can create a new data frame called final that selects only these columns.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"],
)
fig
variable you defined earlier.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(debug=True)
python3 app.py
200
Dash is running on http://127.0.0.1:8050/
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
200