Cannot connect to my Colab

What I’m trying to do:
Link my Anvil app to my Google Colab.

What I’ve tried and what’s not working:

  • Checked previous posts

Code Sample:
This is what I have in my notebook:

import anvil.server

@anvil.server.callable
def answer(query):


  answer = "Hello World"
  return answer

#Connect anvil
import anvil.server
anvil.server.connect("server_KRHVYBDACJ2PUFOGGHM7ZXNP-NLRZEEIEXF6WKSPE")
anvil.server.wait_forever()

I get this message so it seems that the notebook is working:

Connecting to wss://anvil.works/uplink
Anvil websocket open
Connected to “Default Environment” as SERVER

This is my code in Anvil:

  def text_box_1_pressed_enter(self, **event_args):
    return_text = anvil.server.call('answer_query', self.text_box_1.text)

Clone link:
share a copy of your app

Welcome to the forum!

So, just off the top of my head, you have a mismatch here. Your server function is answer but you’re calling a server function named answer_query. Those two names should match.

1 Like

Thanks @jshaffstall for your reply.
I correct it to

def answer_query(query):

but I still get the same error.

What is the actual error you’re getting? You never did post the error message.

You are right. This is the error:

Warning: No method “text_area_1_change” in form HomeForm: cannot set the ‘change’ event handler of self.text_area_1.

anvil.server.UplinkDisconnectedError: The uplink server for "answer_query" has been disconnected

  • at HomeForm, line 63

I’m not a colab person, so maybe some of the people who use colab regularly can spot something wrong in your colab code, but it looks okay to me, which suggests that your colab isn’t running anymore.

1 Like

Thanks @jshaffstall actually you helped me a lot. I changed the code in my colab and it’s working now.

Old code (one code cell):

import anvil.server

@anvil.server.callable
def answer(query):


  answer = "Hello World"
  return answer

#Connect anvil
import anvil.server
anvil.server.connect("XXXX")
anvil.server.wait_forever()

New code:
Code cell:

!pip install anvil-uplink

Code cell:

import anvil.server

Code cell:

anvil.server.connect("XXXX")

Code cell:

@anvil.server.callable
  def answer_query(query):

And finally last code cell:

anvil.server.wait_forever()

I am not sure why, but by splitting the code to different code cells it worked.

Thanks again

2 Likes

Check in the props of your UI form as it looks like you have a change event registered as well as the pressed enter but it’s not hooked up to anything.