Last active
December 14, 2021 04:03
-
-
Save xhluca/7f399798c4b383486ed0c5b028ddd695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base64 import b64encode | |
import dash | |
import dash_table | |
import dash_html_components as html | |
import dash_core_components as dcc | |
from dash.dependencies import Input, Output | |
import numpy as np | |
import pandas as pd | |
app = dash.Dash(__name__) | |
server = app.server | |
my_dfs = { | |
"small": pd.DataFrame(np.random.randint(50, size=(10, 5))), | |
"large": pd.DataFrame(p.random.randint(50, size=(50000, 5))), | |
"do not click": pd.DataFrame(data=np.random.randint(50, size=(500000, 10))), | |
"do not click": pd.DataFrame(data=np.random.randint(50, size=(5000000, 10))), | |
} | |
app.layout = html.Div( | |
[ | |
dcc.RadioItems( | |
id="choose-file", | |
options=[{"label": x, "value": x} for x in my_dfs], | |
value="small", | |
), | |
dcc.Loading( | |
html.A(html.Button("Download"), id="download", download="data.csv") | |
), | |
] | |
) | |
@app.callback(Output("download", "href"), [Input("choose-file", "value")]) | |
def table_to_csv(val): | |
df = my_dfs[val] | |
df_b64 = b64encode(df.to_csv(index=False).encode()) | |
return "data:text/csv;base64," + df_b64.decode() | |
if __name__ == "__main__": | |
app.run_server(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dash | |
import dash_table | |
import dash_html_components as html | |
import pandas as pd | |
df = pd.DataFrame( | |
[ | |
["California", 289, 4395, 15.3, 10826], | |
["Arizona", 48, 1078, 22.5, 2550], | |
["Nevada", 11, 238, 21.6, 557], | |
["New Mexico", 33, 261, 7.9, 590], | |
["Colorado", 20, 118, 5.9, 235], | |
], | |
columns=["State", "# Solar Plants", "MW", "Mean MW/Plant", "GWh"], | |
) | |
app = dash.Dash(__name__) | |
server = app.server | |
app.layout = dash_table.DataTable( | |
id="table", | |
columns=[{"name": i, "id": i} for i in df.columns], | |
data=df.to_dict("records"), | |
export_format="csv", | |
) | |
if __name__ == "__main__": | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment