Python Forum
CGI in python, problem with pandas, plotly.express and export html
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CGI in python, problem with pandas, plotly.express and export html
#1
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 !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas : problem with conditional filling of a column Xigris 2 635 Jul-22-2023, 11:44 AM
Last Post: Xigris
  How to express null value klatlap 3 860 Mar-25-2023, 10:40 AM
Last Post: klatlap
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 930 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 931 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Plotly send pandas.errors.UndefinedVariableError. Frankduc 3 1,390 Oct-27-2022, 02:26 PM
Last Post: deanhystad
  export into excel, how to implement pandas into for-loop deneme2 6 2,460 Sep-01-2022, 05:44 AM
Last Post: deneme2
  binning_endpoints ->plotly Luis_liverpool 0 838 Aug-09-2022, 10:13 AM
Last Post: Luis_liverpool
Question Export Python output to Excel skyline1397 1 2,045 Jun-26-2022, 05:10 AM
Last Post: skyline1397
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,307 Aug-29-2021, 12:39 PM
Last Post: snippsat
  reading html and edit chekcbox to html jacklee26 5 3,079 Jul-01-2021, 10:31 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020