Python Forum

Full Version: CGI in python, problem with pandas, plotly.express and export html
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello ! Smile

I need some help with my script in Python. I've two questions :


First question :


I'm creating a CGI which allow to display graph from some data.

The datas looks like :

2020-01-13-00-00,384.00,350.00
2020-01-13-06-00,384.00,350.00
2020-01-13-12-00,384.00,350.00
2020-01-13-18-00,384.00,350.00
2020-01-14-00-00,384.00,350.00
2020-01-14-06-00,384.00,350.00
2020-01-14-12-00,384.00,350.00

I use the ipywidgets, pandas and plotly.express librarys in order to create a web page which will display a graph with two buttons : the first to display the graph and the second to display the graph with the trendline.

My script is :

import ipywidgets as widgets
import pandas as pd
import plotly.express as px
import os
 
####### Button 1 ########
 
button = widgets.Button(description="graph")
display(button)
 
def graph():
    df = pd.read_csv('/xxx/xxx/xxx/Test.txt')
    df.head()
    fig = px.line(df, x = 'Date', y ='Total Used', title='DF command graph')
    fig.update_traces(mode='markers')
    fig.show();
 
output = widgets.Output()
 
@output.capture()
def on_button_clicked(b):
    graph();
 
button.on_click(on_button_clicked)
display(output)


####### Button 2 #########
 
button2 = widgets.Button(description='trend')
display(button2)
 
def trend():
    df = pd.read_csv('/xxx/xxx/xxx/Test.txt')
    df.head()
    fig = px.scatter(float(df), x="Date", y="Total Used", trendline="ols")
    fig.show()
 
output2 = widgets.Output()
 
@output2.capture()
def on_button2_clicked(b):
    trend()
 
button2.on_click(on_button2_clicked)
display(output2)
 
graph()
The first step ( displaying the grap ) works prefectly :

[Image: uc9s.png]

The graph button is here in order to redisplay the original graph withtout the trendline. But when I press the Trend button, I've this error :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/ipywidgets/widgets/widget_output.py in inner(*args, **kwargs)
    101                     self.clear_output(*clear_args, **clear_kwargs)
    102                 with self:
--> 103                     return func(*args, **kwargs)
    104             return inner
    105         return capture_decorator

<ipython-input-17-8774359d7459> in on_button2_clicked(b)
     41 @output2.capture()
     42 def on_button2_clicked(b):
---> 43     trend()
     44 
     45 button2.on_click(on_button2_clicked)

<ipython-input-17-8774359d7459> in trend()
     34     df = pd.read_csv('/xxx/xxx/Desktop/Test.txt')
     35     df.head()
---> 36     fig = px.scatter(float(df), x="Date", y="Total Used", trendline="ols")
     37     fig.show()
     38 

TypeError: float() argument must be a string or a number, not 'DataFrame'
I think this error appears because pandas doesn't understand the part of data :
2020-01-13-00-00
But I don't understand how to fix it... Could you show me how to change that ?


Second question :


I made an other python script and I've succeeded to export my interactif graph like that :

import pandas as pd
import plotly.express as px


df = pd.read_csv('xxx/Test.txt')
df.head()

fig = px.line(df, x = 'Date', y ='Total Used', title='DF command graph')
fig.add_scatter(x=df['Date'], y=df['Free'])

fig.update_traces(mode='markers+lines')
fig.write_html("/xxx/xxx/xxx/file.html")
fig.show()
There is a way to export my script with my two buttons like that ? I don't want to use jupyter notebook but I don't know if it's possible...

Thanks !