importasyncio# (1)!importhttpximportplotly.graph_objectsasgo# (2)!# API endpointtable_id=42328endpoint_url=f"https://sciglass.uni-jena.de/api/gf/{table_id}"headers={"Accept":"application/json","Content-type":"application/json","Authorization":"Bearer $SCIGLASS_NEXT_API_KEY"}asyncdefexample():asyncwithhttpx.AsyncClient(timeout=None)asclient:r=awaitclient.get(endpoint_url,headers=headers)ifr.status_code==200:data=r.json()print(data)# Extract component namescomp_name=[item.split("|")[1]foritemindata['varComp'].split("\\")[:3]]# Helper function to parse trace datadefparse_trace(trace):values=trace['value'].split('')a=[]# vertex at topb=[]# vertex at leftc=[]# vertex at rightforvalueinvalues:ifvalue.strip()!="":first_value,second_value=map(float,value.split())c.append(first_value)a.append(second_value)b.append(100-first_value-second_value)returna,b,c# Extract and prepare tracestraces=[]fortraceindata['traces']:a,b,c=parse_trace(trace)traces.append({'a':a,'b':b,'c':c,'legend':trace['legend'],'type':trace['type']})# Create Plotly figurefig=go.Figure()# Add traces to the figurefortraceintraces:mode='lines'fill='none'dash_type='solid'legend_name=trace['legend']opacity=1.0iftrace['type']=='P':mode='markers'eliftrace['type']=='Z':fill='toself'legend_name=''# don't give the filled area nameopacity=0.3eliftrace['type']=='D':fill='toself'opacity=0.3eliftrace['type']=='N':dash_type='dash'fig.add_trace(go.Scatterternary(a=trace['a'],b=trace['b'],c=trace['c'],mode=mode,name=legend_name,line=dict(dash=dash_type),fill=fill,opacity=opacity,hovertemplate=f'{comp_name[1]}: %{{a}}<br>{comp_name[2]}: %{{b}}<br>{comp_name[0]}: %{{c}}<extra></extra>'))# Update layout with axis titlesfig.update_layout({'ternary':{'sum':100,'aaxis':{'title':{'text':comp_name[1]}},'baxis':{'title':{'text':comp_name[2]}},'caxis':{'title':{'text':comp_name[0]}},},'title':'Ternary Plot'})# Show figurefig.show()else:print(f"Request failed with status code: {r.status_code}")print("Response text:",r.text)asyncio.run(example())
AsyncIO is Python's built-in library for writing concurrent code with the async/await syntax.
This is optional and is imported in this example to show how to visualize the result using Plotly.
The ternary plot direction cannot be changed in Plotly and differs from the SciGlass convention. See the Python example code for details on how to parse the data points.
Note
Data points are stored in the value key,separated by a special delimiter \x7f.
The first value means the value of vertex at right (C).
The second value means the value of vertex at top (A).
The value of (100 - A - C) means the value of vertex at left (B).