Plotly padding is not working for me - simple app cloned

Tried responding to the post about padding on plotly padding but that’s an old post and I don’t think it will get the attention I need. I have a really simple test app with a plot (included as a clone). The python code suggested as a way to reduce the padding around a plotly graph is having no affect at all.

    self.tp.layout.margin.t = 80 
    self.tp.layout.margin.b = 80
    self.tp.layout.margin.l = 80
    self.tp.layout.margin.r = 80
    self.tp.layout.yaxis.range=[0,2]
    self.tp.interactive=False
'''
https://anvil.works/build#clone:2VDM2KBSBX5AR6FQ=VTZN7M2VJXWMKBL7PMCA6TML

The adjustment of the padding as described in the documentation, self.tp.layout.margin.t = 70 for example, is having no impact at all on my plots. I have set it to 0, 10, all the way to 70 - no impact.

from ._anvil_designer import Form1Template
from anvil import *
import plotly.graph_objects as go
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from plotly import graph_objs as go

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    self.tp.layout.margin.t = 70
    self.tp.layout.margin.b = 70
    self.tp.layout.margin.l = 70
    self.tp.layout.margin.r = 70
    
    self.tp.layout = {
       'titlefont': {
       "size": 14
       }
    }
    
    self.tp.layout.title=({
        'text': 'Threat Beta vs. Industry Peers',
        'xanchor': 'center',
        'yanchor': 'top',
        })
    
    self.tp.layout.yaxis.range=[0,2]
    self.tp.interactive=False
        
    #Create and style traces
    TargetTb = ['',.67,1.34]
    PeerTb = ['',1.03,1.67]
    
    trace0 = go.Scatter(
        x = ['February','March', 'April'],
        y = TargetTb,
        name = 'Peers',
        line = dict(
            color = ('rgb(205, 12, 24)'),
            width = 1,
            dash = 'solid')
    )
    trace1 = go.Scatter(
        x = ['February','March', 'April'],
        y = PeerTb,
        name = 'You',
        line = dict(
            color = ('rgb(22, 96, 167)'),
            width = 1,
            dash = 'solid')
    )
      
    self.tp.data = [trace0,trace1]

We keep an eye on all the posts. As long as they have clear questions, and are relevant to their thread, they will have a good chance of receiving attention.

Please advise if there is anything unclear about this post, or the clone app, not sure why the clone app icon is not appearing

Nothing unclear here. Your link isn’t working because your closing backtics are not backtics but apostrophes. No problem.

Works fine for me. You are setting the margins twice and therefore overwriting the initial settings.
ezgif.com-gif-maker (6)

Humor me, rename that plot from plot_1, to tp, and see if it still works.

I created a new app, followed the tutorial, left the plot named plot_1, and I noticed the syntax helper did indeed show me margins as options on layout. In my app, I had the plot named ‘tp’ for ‘top plot’… syntax helper did not show margin as a valid option.

More testing, my second plot was named ‘ndist’ for normal distribution… again, margin not an available option in the syntax helper… I renamed that plot “plot_2” - all works fine.

The autocomlete issue seems like it has a bug.

Based on your clone though, this was not the reason why the margins were not responding.

The devs will notice this thread and fix things.

Did you get this sorted?

I think you override margin when you do
layout =

So you could use layout.update instead

I’ll try that, I tried following examples that indicated use of layout.update… but the interpreter responded that update was not a valid method - now that I have the plot named plot_n… the interpreter is not balking at that.

I am getting an error that’s stopping everything… ‘server code took to long…’ Is there a parameter somewhere I can examine/adjust?

As I mention, the margin seems to work as expected on my end. Autocomplete issue is very real though.

1 Like

Right, the issue now is that the server code is timing out

:ok_hand:.

Server code is 30 seconds max. If you go above that you’ll need to explore background tasks.

Concerning the server timeouts, If you are able to share clone, perhaps we can help debug why things are taking so long.

Here’s a clone:
I’ve modified the code to use a background task, and that seems to work just fine. App explained, and yes, it’s likely a mess!

From the homepage, I click ‘upload’ - the file uploader loads a .csv and passes that to the server. The server module reads each ‘row’, extracts the requisite data, creates a .pdf, and stores that in a table. When finished, control passes back to the homepage, which reads each row of the table and downloads the .pdfs from the table.

not sure how to attach my sample data .csv file… Ultimately I’ll be processing over 9000 .pdfs… so probably better that I moved it to a background task.

https://anvil.works/build#clone:XJVULVEJLGG26VJZ=V76MCRPL4QTHEWUBRQPLQZQW

Oh I see. Based on the topic here I thought your Plotly code was somehow causing server timeouts, but it sounds like you are just processing a lot of data in general.

Yes, so background tasks was going to be necessary anyway. Listen, I have to communicate with Anvil writ large - were it not for Anvil, our analytics startup would simply not be making the progress we are, it is THE instrument that is letting us deliver. I’ll have to find a way to write up a testimonial or something.

Thanks for all your help.

Just a general comment having looked very quickly at the client-side code. This may be a moot point in some respects; however, I wonder if your app would perform much faster if you avoided putting server calls inside of loops. It may be worth it to send your list of files to a single server call and do the rest of the processing server-side. Similarly, I wonder if, rather than looping to download a PDF (based on a connection to a live row object which hits the server), you would have better performance if you packaged all your PDFs up into a zip file (on the server) and allowed the client to download that. My understanding is that you would have far fewer round trips this way.

1 Like

Definitely makes sense… I figured I was going to run into performance-related issues