Anvil.server.wait_forever() not working

I’m trying to run the anvil.server.wait_forever() function at the end of my Google Colab code to connect with my app for the first time, but it wont finish running and display "
Connecting to wss://anvil.works/uplink
Anvil websocket open
Authenticated OK"
(Like indicated in the tutorial.)

I’ve tried running it on the “iris-classifier-start.ipynb” tutorial as well without straying away from the instructions at all, and the same thing is happening.

Tutorial:

!pip install anvil-uplink
import anvil.server
anvil.server.connect(".....")
import matplotlib.pyplot as plt
from sklearn import metrics
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 , random_state=4)

# The following code is used only when needing to find the optimal n_neighbors
"""
scores = {}
scores_list = []
k_range = range(1, 26)
for k in k_range:
  knn = KNeighborsClassifier(n_neighbors=k)
  knn.fit(X_train, y_train)
  y_pred = knn.predict(X_test)
  scores[k] = metrics.accuracy_score(y_test, y_pred)
  scores_list.append(metrics.accuracy_score(y_test, y_pred))

plt.plot(k_range,scores_list) 
plt.xlabel('Value of K for KNN')
plt.ylabel('Testing Accuracy')
"""

knn = KNeighborsClassifier(n_neighbors=10)
knn.fit(X,y)
@anvil.server.callable
def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
  classification = knn.predict([[sepal_length, sepal_width, petal_length, petal_width]])
  return iris.target_names[classification][0]
anvil.server.wait_forever()

In your actual notebook, did you put your uplink key rather than "....." as the argument to anvil.server.connect()? Also, are you getting any output at all? (This is the tutorial you’re referring to, right? Turning a Google Colab notebook into a web app)

It’s also worth adding that anvil.server.wait_forever() is nothing more than a way to keep the script running. It’s not actually required to get an uplink script to connect. But it is a useful shortcut to keep your uplink code ‘alive’ - related docs.

It might be better to run the cell without anvil.server.wait_forever().

Then once the cell has executed run anvil.server.wait_forever() in the next cell.
Since anvil.server.wait_forever() is designed to keep the script running this cell will never finish its execution.

2 Likes