-2

I wrote the following code to generate a couple of charts using the plotly library:

#Read the excel file from local spreadsheet
df = pd.read_excel(r'C:\Users\llopez\Documents\Temp\Python finished goods.xlsx', sheet_name='Sheet1')

#Create a dictionary of tuples by year
dict_of_fgs = dict(tuple(df.groupby('Year')))

#Loop through the elements of the dictionary of finished goods and plots them grouping FG, type and calculating the sum of quantities. Stacking by type
for k in dict_of_fgs:
    df_grouped = dict_of_fgs[k].groupby(['Finished_Good','Type'],sort=False)['Qty'].sum().unstack('Type').iplot(kind='bar',barmode='stack', title='FG Chart')

It yields the following charts:

FG Chart 2017FG Chart 2018

The charts are exactly what I need, so now I'm looking for a way to generate a web-based report and share it with users.

I looked into the following tutorial but I can't seem to adapt my code to this since my code loops through a dictionary and generates a plot for each key(Year).

How can I generate a web-based report given the charts I've created?

Thanks in advance to all of you subject matter experts for all the great ideas on how to achieve this. I appreciate the help.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
Luis Lopez
  • 23
  • 5
  • Don’t know if this helps, but I came across this a few weeks ago. [highcharts](https://simpleisbetterthancomplex.com/tutorial/2018/04/03/how-to-integrate-highcharts-js-with-django.html) – Carl Brubaker Jun 05 '18 at 04:07

2 Answers2

0

If I understood correctly, you want the Plotly charts in an HTML report or dashboard.

If that's the case, then you should have a look at Rmarkdown in RStudio. Even though it's Rmardown rather than Pymarkdown, it does support Python.

Having some exposure to R would help, but if it's not the case, don't worry, you can get your HTML report using only Python and Markdown.

You need to install RStudio, and the Rmarkdown, knitr, and reticulate packages using:

  1. install R
  2. install RStudio (there is a free version)
  3. Run the following command to install the needed packages:

    install.packages(c('Rmarkdown', 'knitr', 'reticulate'))
    
  4. You can create your Rmarkdown doc and generate the HTML report.

Some doc here:

And another question I answered some time ago, about generating a Plotly plot in python and inserting it using R in the HTML report: https://stackoverflow.com/a/50194462/2699660

If you don't want to use R, then you can as in the answer above, generate the HTML plot locally in offline mode, and insert it into the HTML report using an <iframe>, e.g.:

<iframe src="path_to_your_plot.html"
    height="600" width="100%"
    scrolling="no" seamless="seamless"
    frameBorder="0">
</iframe>
byouness
  • 1,746
  • 2
  • 24
  • 41
-1

a simple answer is to use matplotlib savefig to generate an image and serve that to your web site

savefig('foo.png')

as explained: Save plot to image file instead of displaying it using Matplotlib

Otherwise, for dynamic charts, you'll need to send the data to the browser with an API, (i.e. your web site needs to call the API to get the data) and then use some chart library (or build your own) to display the chart with Javascript in the HTML page.

MrE
  • 19,584
  • 12
  • 87
  • 105