Create Gantt chart
Tags: #plotly #chart #gant #project #dataviz #snippet
Author: Jeremy Ravenel
Learn more on the Plotly doc : https://plotly.com/python/gantt/

Input

Import libraries

1
import plotly.express as px
2
import pandas as pd
Copied!

Model

Get data

1
df = pd.DataFrame([
2
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
3
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
4
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
5
])
6
df
Copied!

Create the plot

1
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
2
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
3
fig.update_layout(
4
plot_bgcolor="#ffffff",
5
width=1200,
6
height=600)
7
config = {'displayModeBar': False}
8
fig.show(config=config)
Copied!

Output

Export in PNG and HTML

1
fig.write_image(output_image, width=1200)
2
fig.write_html(output_html)
Copied!

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub