-
-
Save chriddyp/3d2454905d8f01886d651f207e2419f0 to your computer and use it in GitHub Desktop.
# See official docs at https://dash.plotly.com | |
# pip install dash pandas | |
from dash import Dash, dcc, html, Input, Output | |
import plotly.express as px | |
import pandas as pd | |
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv') | |
app = Dash(__name__) | |
app.layout = html.Div([ | |
dcc.Graph(id='graph-with-slider'), | |
dcc.Slider( | |
df['year'].min(), | |
df['year'].max(), | |
step=None, | |
value=df['year'].min(), | |
marks={str(year): str(year) for year in df['year'].unique()}, | |
id='year-slider' | |
) | |
]) | |
@app.callback( | |
Output('graph-with-slider', 'figure'), | |
Input('year-slider', 'value')) | |
def update_figure(selected_year): | |
filtered_df = df[df.year == selected_year] | |
fig = px.scatter(filtered_df, x="gdpPercap", y="lifeExp", | |
size="pop", color="continent", hover_name="country", | |
log_x=True, size_max=55) | |
return fig | |
if __name__ == '__main__': | |
app.run_server(debug=True) |
how to run a dash application can anybody tell me coz i am getting an error while running the code on jupyter i.e. system exit and also tell me from where i can access the app
use the command prompt in running your code also i dont advise you use jupyter for coding or running dash.
on your command promt just navigate to your file.
ALSO
this worked for me:
just click the file it's going to run as executable
I'm running the above code using Spyder/Anaconda. When opening local host on port 8050, I only see a White window with "cannot connect..." on the top. Can anyone please help me?
Hi, can anyone tell why google isnt working and yahoo is?
Hi, can anyone tell why google isnt working and yahoo is?
apparently google is not available as a data source anymore, but yahoo still is pydata/pandas-datareader#768
Can confirm that as of 2022 google doesn't work but yahoo does.
Hello there,
I just have a quick question. Is there a way to export the resulting report/dashboard as a standalone html file (something with all assets bundled), or does dash require some server-side processing via python? I have seen some efforts to get python running in the browser via webassembly; however, I am not sure if dash already supports this out of the box or not.
I have been using a combination of Rmarkdown/quarto with python and plotly to create dashboard-like reports. The nice thing with Rmarkdown/quarto is that you can export everything as a stand alone html file, which is nice when working with collaborators. Just email them a report, and they can open it with Chrome.
Anyways, please let mw know what you think, and have a great day!
Best Regards,
@skchronicles
Working example:
from dash import Dash, dcc, html, callback
from dash.dependencies import Input, Output
import yfinance as yf
start = '2022-01-01'
end = '2023-01-01'
app = Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id='my-dropdown',
options=[
{'label': 'MSFT', 'value': 'MSFT'},
{'label': 'AAPL', 'value': 'AAPL'},
],
value='MSFT'
),
dcc.Graph(id='my-graph')
], style={'width': '500'})
@callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
df = yf.download(selected_dropdown_value, start=start, end=end)
return {
'data': [{
'x': df.index,
'y': df.Close
}],
'layout': {'margin': {'l': 40, 'r': 0, 't': 20, 'b': 30}}
}
app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
if __name__ == '__main__':
app.run(debug=True)
@aboucher410 please look at the basics of python before putting the hands on this code.
The error you see is because you do not have the library pandas installed.
Look up at how to use virtualenvironments and pip. From a Terminal you would need to type the following commands (assuming you are on linux, and you have admin permissions):
Now, save all the required libraries in a file called requirements.txt, in the same folder where you have the python module above, one library for each line. It would look like:
Continue from the terminal:
There is a lot, and few details of why. Do not be discouraged if something does not work at the first attempt. Look up each problem you may encounter and try to solve it with google. In any case, please read the documentation about pip and virtualenv to get the basics, and in particular to grasp the reasons why we need them.
Hope it helps.