|
| 1 | +import dash |
| 2 | +from dash import dcc, html |
| 3 | +import plotly.express as px |
| 4 | +import plotly.graph_objects as go |
| 5 | +import pandas as pd |
| 6 | +import numpy as np |
| 7 | +import requests |
| 8 | + |
| 9 | +# Your safe log10 function |
| 10 | +def safe_log10(x): |
| 11 | + try: |
| 12 | + val = float(x) |
| 13 | + return np.log10(val) if val > 0 else np.nan |
| 14 | + except (TypeError, ValueError): |
| 15 | + return np.nan |
| 16 | + |
| 17 | +# Function to get data from API |
| 18 | +def api_pg_dataset_linechart(url_dataset, url_reference_elements, log10=True): |
| 19 | + response_ref = requests.get(url_reference_elements) |
| 20 | + ref_elements = response_ref.json() |
| 21 | + ref = pd.DataFrame(ref_elements) |
| 22 | + |
| 23 | + response_data = requests.get(url_dataset) |
| 24 | + data = response_data.json() |
| 25 | + df = pd.DataFrame(data) |
| 26 | + |
| 27 | + new_column_order = ref["symbole"].str.lower().tolist() |
| 28 | + ordered_df = df[[col for col in new_column_order if col in df.columns]] |
| 29 | + |
| 30 | + if log10: |
| 31 | + df_log10 = ordered_df.applymap(safe_log10) |
| 32 | + else: |
| 33 | + df_log10 = ordered_df |
| 34 | + |
| 35 | + return { |
| 36 | + "data": df, |
| 37 | + "elements": df_log10 |
| 38 | + } |
| 39 | + |
| 40 | +# Fetch data once for this example (could be dynamic) |
| 41 | +urls = { |
| 42 | + "dataset": "http://157.136.252.188:3000/dataset_adisser17", |
| 43 | + "reference": "http://157.136.252.188:3000/ref_elements" |
| 44 | +} |
| 45 | +result = api_pg_dataset_linechart(urls["dataset"], urls["reference"], log10=True) |
| 46 | +df_log = result["elements"] |
| 47 | + |
| 48 | +# Create the Dash app |
| 49 | +# app = dash.Dash(__name__) |
| 50 | +app = dash.Dash(__name__, requests_pathname_prefix='/dash/') |
| 51 | + |
| 52 | + |
| 53 | +# Build the line chart using Plotly |
| 54 | +fig = go.Figure() |
| 55 | +for idx, row in df_log.iterrows(): |
| 56 | + fig.add_trace(go.Scatter( |
| 57 | + x=df_log.columns, |
| 58 | + y=row.values, |
| 59 | + mode='lines+markers', |
| 60 | + name=str(idx) |
| 61 | + )) |
| 62 | + |
| 63 | +fig.update_layout(title="dataset_adisser17", |
| 64 | + xaxis_title="Element", |
| 65 | + yaxis_title="Log10 Value") |
| 66 | + |
| 67 | +# App Layout |
| 68 | +app.layout = html.Div([ |
| 69 | + html.H1("CHIPS Dashboard"), |
| 70 | + dcc.Graph(figure=fig) |
| 71 | +]) |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + app.run(debug=False, host='0.0.0.0', port=8050) |
| 75 | + |
0 commit comments