Log server endpoints

I try to log data in my server endpoint (extending the advent calendar caller project), but logs don’t show up in the console. Is there another way to capture what goes on in these functions? Ideally i want to push out another message via the kwargs or perhaps using /call-connected/:msg or something. the msg passed could come from a data table of messages.

@anvil.server.http_endpoint("/call-connected/")
def call_connected( **kwargs):
    print("Hello from the server side!")
    return """<Response><Say>Hi i want to print a message passed in as a parameter here </Say></Response> """
1 Like

Welcome, paco! Glad to have you aboard.

You may be interested in this post:

1 Like

Thanks, I was able to find the App Logs, which were unknown to me until today. However my main problem remains with these endpoints. They don’t seem to be able to access global modules. I can pass data to the server from the client side and store it in a global module, but the endpoint doesn’t see this data. it sees the dummy text that I initialized the variable to in the global module.

Let’s think about this for a minute.

Assume, for the moment, that 10 different users are logged in to your app, so there are 10 distinct client instances running, each in a different browser. When an endpoint receives a request, at the server, which one of those 10 clients’ data should it see? How should it decide?

Actually, since an endpoint could receive a request from anyone, anywhere, the request is not tied to any specific client. Anvil starts up a brand-new instance of your server program, and routes the request to that instance. No client required.

The same happens with ordinary anvil.server.call requests from a client. An instance starts up, and handles the request. Each instance has its own memory area, so that information from different clients does not get mixed together.

Now, any server instance is likely to shut down in a matter of seconds, to let other server instances run. So server-side global variables from any one client may not live for very long.

To help you deal with that, there is one special server-side variable, which does persist between calls, one instance per connection. This variable is named anvil.server.session.

If you want to pass information between server program instances, you will need something that outlives these instances. There is such a thing. Database table rows outlive all instances.

So to pass information, you will want to put it in a database table, with enough identifying information that a later instance can find it.

1 Like

Great, this makes good sense and of course, a reason to avoid going down the global variable rabbit hole with these endpoints. The data table solution seems doable. I already have a list of messages there that I want to send, given the user choosing the outgoing message in the client side. These are going to be Twilio calls with the customized message. However it seems that I can either create a new table called “calls” and store the user_id and the message chosen there. Kind of like a log of calls. the end point would just load the latest row of these calls table and it should contain the chosen message. However if this had multiple requests coming in simultaneously, this would not work.

I was interested in doing what is show in this page:

for example, adding a parameter to the endpoint so we would know what message to load:

    @anvil.server.http_endpoint("/call-connected/:msg_id")
    def call_connected(msg_id, **kwargs):
        # get the msg from the data table by msg_id

        return "<Response><Say>{msg}</Say><Response>"

The problem is that in my server function that is called from the client, I can’t build the request URL with the added parameter. I’m not sure if it’s Twilio or Anvil that throws the error.

@anvil.server.callable
def make_call(number, msg_id):
  
  twilio_acct_id = anvil.secrets.get_secret('twilio_id')
  twilio_auth_token = anvil.secrets.get_secret('twilio_auth_token')
  twilio_from_number = anvil.secrets.get_secret('twilio_phone_number')
   

  anvil.http.request(
    f"https://api.twilio.com/2010-04-01/Accounts/{twilio_acct_id}/Calls.json",
      data={
      
        "Url": f"{anvil.server.get_api_origin()}/call-connected/",
        "From" : twilio_from_number,
        "To": number,
      },
    method = "POST",
    username = twilio_acct_id,
    password = twilio_auth_token
    
  )

adding "Url": f"{anvil.server.get_api_origin()}/call-connected/{msg_id}"
doesn’t work.

any ideas? and thanks for your support. I should say, I’m actually trying to build lesson plans around this for a program for high school students. so you are not just helping me, but potentially lots of kids who will get their chance to make full stack apps with python! so excited.

Well, seems like I solved this one. My error is that the URL could not accept underscores (msg_id) ! Thanks again

2 Likes