|
| 1 | +import plotly.graph_objects as go |
| 2 | +import plotly.figure_factory as ff |
| 3 | + |
| 4 | +from trame.app import get_server |
| 5 | +from trame.ui.vuetify import SinglePageLayout |
| 6 | +from trame.widgets import vuetify, plotly |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +# ----------------------------------------------------------------------------- |
| 12 | +# Trame setup |
| 13 | +# ----------------------------------------------------------------------------- |
| 14 | + |
| 15 | +server = get_server() |
| 16 | +server.client_type = "vue2" |
| 17 | +state, ctrl = server.state, server.controller |
| 18 | + |
| 19 | +# ----------------------------------------------------------------------------- |
| 20 | +# Charts handling |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +contour_raw_data = pd.read_json( |
| 24 | + "https://raw.githubusercontent.com/plotly/datasets/master/contour_data.json" |
| 25 | +) |
| 26 | +scatter_raw_data = pd.read_json( |
| 27 | + "https://raw.githubusercontent.com/plotly/datasets/master/scatter_data.json" |
| 28 | +) |
| 29 | +polar_data = pd.read_csv( |
| 30 | + "https://raw.githubusercontent.com/plotly/datasets/master/polar_dataset.csv" |
| 31 | +) |
| 32 | + |
| 33 | +scatter_data = scatter_raw_data["Data"] |
| 34 | + |
| 35 | + |
| 36 | +def clean_data(data_in): |
| 37 | + """ |
| 38 | + Cleans data in a format which can be conveniently |
| 39 | + used for drawing traces. Takes a dictionary as the |
| 40 | + input, and returns a list in the following format: |
| 41 | +
|
| 42 | + input = {'key': ['a b c']} |
| 43 | + output = [key, [a, b, c]] |
| 44 | + """ |
| 45 | + key = list(data_in.keys())[0] |
| 46 | + data_out = [key] |
| 47 | + for i in data_in[key]: |
| 48 | + data_out.append(list(map(float, i.split(" ")))) |
| 49 | + |
| 50 | + return data_out |
| 51 | + |
| 52 | + |
| 53 | +def create_ternary_fig(**kwargs): |
| 54 | + contour_dict = contour_raw_data["Data"] |
| 55 | + |
| 56 | + # Defining a colormap: |
| 57 | + colors = [ |
| 58 | + "#8dd3c7", |
| 59 | + "#ffffb3", |
| 60 | + "#bebada", |
| 61 | + "#fb8072", |
| 62 | + "#80b1d3", |
| 63 | + "#fdb462", |
| 64 | + "#b3de69", |
| 65 | + "#fccde5", |
| 66 | + "#d9d9d9", |
| 67 | + "#bc80bd", |
| 68 | + ] |
| 69 | + colors_iterator = iter(colors) |
| 70 | + |
| 71 | + fig = go.Figure() |
| 72 | + |
| 73 | + for raw_data in contour_dict: |
| 74 | + data = clean_data(raw_data) |
| 75 | + |
| 76 | + a = [inner_data[0] for inner_data in data[1:]] |
| 77 | + a.append(data[1][0]) # Closing the loop |
| 78 | + |
| 79 | + b = [inner_data[1] for inner_data in data[1:]] |
| 80 | + b.append(data[1][1]) # Closing the loop |
| 81 | + |
| 82 | + c = [inner_data[2] for inner_data in data[1:]] |
| 83 | + c.append(data[1][2]) # Closing the loop |
| 84 | + |
| 85 | + fig.add_trace( |
| 86 | + go.Scatterternary( |
| 87 | + text=data[0], |
| 88 | + a=a, |
| 89 | + b=b, |
| 90 | + c=c, |
| 91 | + mode="lines", |
| 92 | + line=dict(color="#444", shape="spline"), |
| 93 | + fill="toself", |
| 94 | + fillcolor=colors_iterator.__next__(), |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + fig.update_layout( |
| 99 | + margin=dict(l=50, r=50, t=50, b=50), |
| 100 | + ) |
| 101 | + return fig |
| 102 | + |
| 103 | + |
| 104 | +def create_polar_fig(**kwargs): |
| 105 | + fig = go.Figure() |
| 106 | + fig.add_trace( |
| 107 | + go.Scatterpolar( |
| 108 | + r=polar_data["x1"].tolist(), |
| 109 | + theta=polar_data["y"].tolist(), |
| 110 | + mode="lines", |
| 111 | + name="Figure 8", |
| 112 | + line_color="peru", |
| 113 | + ) |
| 114 | + ) |
| 115 | + fig.add_trace( |
| 116 | + go.Scatterpolar( |
| 117 | + r=polar_data["x2"].tolist(), |
| 118 | + theta=polar_data["y"].tolist(), |
| 119 | + mode="lines", |
| 120 | + name="Cardioid", |
| 121 | + line_color="darkviolet", |
| 122 | + ) |
| 123 | + ) |
| 124 | + fig.add_trace( |
| 125 | + go.Scatterpolar( |
| 126 | + r=polar_data["x3"].tolist(), |
| 127 | + theta=polar_data["y"].tolist(), |
| 128 | + mode="lines", |
| 129 | + name="Hypercardioid", |
| 130 | + line_color="deepskyblue", |
| 131 | + ) |
| 132 | + ) |
| 133 | + |
| 134 | + fig.update_layout( |
| 135 | + # title = 'Mic Patterns', |
| 136 | + margin=dict(l=20, r=20, t=20, b=20), |
| 137 | + showlegend=False, |
| 138 | + ) |
| 139 | + return fig |
| 140 | + |
| 141 | + |
| 142 | +def create_streamline_fig(**kwargs): |
| 143 | + x = np.linspace(-3, 3, 100) |
| 144 | + y = np.linspace(-3, 3, 100) |
| 145 | + Y, X = np.meshgrid(x, y) |
| 146 | + u = -1 - X**2 + Y |
| 147 | + v = 1 + X - Y**2 |
| 148 | + |
| 149 | + # Create streamline figure |
| 150 | + fig = ff.create_streamline( |
| 151 | + x.tolist(), |
| 152 | + y.tolist(), |
| 153 | + u.tolist(), |
| 154 | + v.tolist(), |
| 155 | + arrow_scale=0.1, |
| 156 | + ) |
| 157 | + fig.update_layout( |
| 158 | + # title = 'Mic Patterns', |
| 159 | + margin=dict(l=20, r=20, t=10, b=10), |
| 160 | + showlegend=False, |
| 161 | + ) |
| 162 | + return fig |
| 163 | + |
| 164 | + |
| 165 | +def create_contour_fig(**kwargs): |
| 166 | + fig = go.Figure( |
| 167 | + data=go.Contour( |
| 168 | + z=[ |
| 169 | + [2, 4, 7, 12, 13, 14, 15, 16], |
| 170 | + [3, 1, 6, 11, 12, 13, 16, 17], |
| 171 | + [4, 2, 7, 7, 11, 14, 17, 18], |
| 172 | + [5, 3, 8, 8, 13, 15, 18, 19], |
| 173 | + [7, 4, 10, 9, 16, 18, 20, 19], |
| 174 | + [9, 10, 5, 27, 23, 21, 21, 21], |
| 175 | + [11, 14, 17, 26, 25, 24, 23, 22], |
| 176 | + ], |
| 177 | + contours={ |
| 178 | + "coloring": "heatmap", |
| 179 | + "showlabels": True, |
| 180 | + "labelfont": { |
| 181 | + "size": 12, |
| 182 | + "color": "black", |
| 183 | + }, |
| 184 | + }, |
| 185 | + line_smoothing=1, |
| 186 | + ) |
| 187 | + ) |
| 188 | + fig.update_layout( |
| 189 | + # title = 'Mic Patterns', |
| 190 | + margin=dict(l=20, r=20, t=10, b=10), |
| 191 | + showlegend=False, |
| 192 | + ) |
| 193 | + return fig |
| 194 | + |
| 195 | + |
| 196 | +# ----------------------------------------------------------------------------- |
| 197 | +# GUI |
| 198 | +# ----------------------------------------------------------------------------- |
| 199 | + |
| 200 | + |
| 201 | +state.trame__title = "Charts" |
| 202 | + |
| 203 | +with SinglePageLayout(server) as layout: |
| 204 | + layout.title.set_text("Many charts") |
| 205 | + |
| 206 | + with layout.content: |
| 207 | + with vuetify.VContainer(fluid=True, classes="fill-height"): |
| 208 | + with vuetify.VRow(style="height: 50%;"): |
| 209 | + with vuetify.VCol(): |
| 210 | + plotly.Figure( |
| 211 | + create_polar_fig(), |
| 212 | + display_mode_bar=("false",), |
| 213 | + ) |
| 214 | + with vuetify.VCol(): |
| 215 | + plotly.Figure( |
| 216 | + create_ternary_fig(), |
| 217 | + display_mode_bar=("false",), |
| 218 | + ) |
| 219 | + with vuetify.VRow(style="height: 50%;"): |
| 220 | + with vuetify.VCol(): |
| 221 | + plotly.Figure( |
| 222 | + create_contour_fig(), |
| 223 | + display_mode_bar=("false",), |
| 224 | + ) |
| 225 | + with vuetify.VCol(): |
| 226 | + plotly.Figure( |
| 227 | + create_streamline_fig(), |
| 228 | + display_mode_bar=("false",), |
| 229 | + ) |
| 230 | + |
| 231 | +# ----------------------------------------------------------------------------- |
| 232 | +# Main |
| 233 | +# ----------------------------------------------------------------------------- |
| 234 | + |
| 235 | +if __name__ == "__main__": |
| 236 | + server.start() |
0 commit comments