|
| 1 | +import glob |
| 2 | +from pathlib import Path, PurePath |
| 3 | +from dash import Dash |
| 4 | +import dash_table |
| 5 | +import dash_core_components as dcc |
| 6 | +import dash_html_components as html |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +p = Path('.') |
| 10 | + |
| 11 | + |
| 12 | +def Add_Dash(server): |
| 13 | + """Populates page with previews of datasets.""" |
| 14 | + external_stylesheets = ['https://hackers.nyc3.cdn.digitaloceanspaces.com/css/plotly-flask-tutorial8.css', |
| 15 | + 'https://fonts.googleapis.com/css?family=Lato', |
| 16 | + 'https://use.fontawesome.com/releases/v5.8.1/css/all.css'] |
| 17 | + dash_app = Dash(server=server, |
| 18 | + url_base_pathname='/plotly_dash_views/', |
| 19 | + external_stylesheets=external_stylesheets) |
| 20 | + dash_app.index_string = '''<!DOCTYPE html> |
| 21 | + <html> |
| 22 | + <head> |
| 23 | + {%metas%} |
| 24 | + <title>{%title%}</title> |
| 25 | + {%favicon%} |
| 26 | + {%css%} |
| 27 | + </head> |
| 28 | + <body> |
| 29 | + <nav> |
| 30 | + <a href="/"><i class="fas fa-home"></i> Home</a> |
| 31 | + <a href="/plotly_dash_views/"><i class="fas fa-chart-line"></i> Embdedded Plotly Dash</a> |
| 32 | + </nav> |
| 33 | + {%app_entry%} |
| 34 | + <footer> |
| 35 | + {%config%} |
| 36 | + {%scripts%} |
| 37 | + {%renderer%} |
| 38 | + </footer> |
| 39 | + </body> |
| 40 | + </html>''' |
| 41 | + |
| 42 | + |
| 43 | + # Create layout |
| 44 | + dash_app.layout = html.Div( |
| 45 | + children=get_datasets(), |
| 46 | + id='flex-container' |
| 47 | + ) |
| 48 | + |
| 49 | + return dash_app.server |
| 50 | + |
| 51 | + |
| 52 | +def get_datasets(): |
| 53 | + """Gets all CSVs in /data directory.""" |
| 54 | + data_filepath = list(p.glob('plotly_flask_tutorial/plotly_dash_views/data/*.csv')) |
| 55 | + arr = ['This is an example Plot.ly Dash App.'] |
| 56 | + for index, csv in enumerate(data_filepath): |
| 57 | + print(PurePath(csv)) |
| 58 | + df = pd.read_csv(data_filepath[index]).head(10) |
| 59 | + table_preview = dash_table.DataTable( |
| 60 | + id='table_' + str(index), |
| 61 | + columns=[{"name": i, "id": i} for i in df.columns], |
| 62 | + data=df.to_dict("rows"), |
| 63 | + sorting=True, |
| 64 | + ) |
| 65 | + arr.append(table_preview) |
| 66 | + return arr |
0 commit comments