0

Is there a way to get rid of extra padding on the right of the image after imgkit from_string method? You can see there is lot of white space on the right. Here is my code to do that . I am using to_html method to export chart with all script that supports the chart render and then from_string of imgkit to convert html to image bytes . Is there any way to get rid of white space on right ?

           fig = go.Figure(data=[go.Bar(
            x=[*data_dict.keys()],
            y=[*data_dict.values()],            
            name='Nodes',            
            marker=dict(color='rgb(128, 0, 128)'),
            # legendrank=1,
        )])
        # set the x-axis position to the top of the plot
        fig.update_layout(xaxis=dict(
            position=0.05,
            tickangle=-90,
            tickfont=dict(size=10),
            tickmode='array',
            tickvals=['A', 'B', 'C'],
        ))
        fig.update_layout(
            # font_color="white",
            font=dict(size=18),
            plot_bgcolor='rgb(0, 0, 0)',
            title=title,
            title_x=0.5,
            title_y=0.9,
            yaxis_title="Page Rank",
            showlegend=True,
            legend=dict(
                orientation="v",
                yanchor="top",
                y=-0.2,
                xanchor="left",
                x=0
            ),
  
        )
        # Convert fig.data tuple to a list, sort it based on x-axis value in ascending order, and convert back to a tuple
        fig.data = tuple(sorted(list(fig.data), key=lambda trace: trace.x[0]))

        fig.update_xaxes(showgrid=True, gridwidth=0.1,
                         gridcolor='rgb(200, 200, 200)')
        fig.update_yaxes(showgrid=True, gridwidth=0.1,
                         gridcolor='rgb(200, 200, 200)')
        # return base64.b64encode(fig.to_image(format='png', width=400, height=400, scale=0)).decode('utf-8')
        html = fig.to_html(
             config={'displayModeBar': False, 'staticPlot': True})
        **img = imgkit.from_string(html, False, options={'format': 'png'})**
        image = base64.b64encode(img).decode('utf-8')

enter image description here

shailesh gavathe
  • 341
  • 3
  • 10
  • Figured it out. It needed to send options to crop at certain dimension. ``` options = { 'format': 'png', 'crop-w': '650', 'quiet': '', 'encoding': "UTF-8" } img = imgkit.from_string(html, False, options) ``` – shailesh gavathe Apr 07 '23 at 15:51

1 Answers1

0

Figured it out. It needed to send options to crop at certain dimension.

  options = {
            'format': 'png',          
            'crop-w': '650',            
            'quiet': '',
            'encoding': "UTF-8"
        }

        img = imgkit.from_string(html, False, options)

This will get rid of White Space to the right.

enter image description here

shailesh gavathe
  • 341
  • 3
  • 10