Trouble with anvil.js.window : 'anvil.js' is not a package

What I’m trying to do:

I am trying to allow an application (Called RSAKPI) I have copy a report (in text) to the clipboard from Anvil installed on a local workstation.

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

I added the package line for anvil.js.window as the examples show:

from ._anvil_designer import RSAKPITemplate
import anvil.server
from .Transport import Cargo
from anvil.js.window import navigator

But when I run the page I get this:

[ERROR anvil.app-server.run] Error report from client code:
ModuleNotFoundError: No module named ‘anvil.js.window’; ‘anvil.js’ is not a package
Traceback:
RSAKPI_Optimisation/RSAKPI.py:4

C:/Users/adrian/miniconda3/envs/rsakpi/lib/site-packages/anvil_downlink_worker/init.py:70

C:/Users/adrian/miniconda3/envs/rsakpi/lib/site-packages/anvil_downlink_worker/init.py:186

app/RSAKPI_Optimisation/RSAKPI/init.py:28

I am running this locally on my own server.

So I thought I would get this working on the Anvil server using a simple example called DataGridPlay (which I did get working on Anvil).

I then saved this in GitHub and cloned it to the workstation I am using.

Using the same command window from MiniConda where I got the error, I killed the RSAKPI app and started the DataGridPlay application.

It works fine.

My levels of knowledge here are very thin, but I thought that if I got one application working with anvil.js.window from a Miniconda environment then any other application launched form there should work too.

Note: I am using VSCode locally with the python interpreter set to the RSAKPI minconda environment. For both applications it gives me warnings that ._anvil_designer and anvil.js.window does not exist. On its on anvil.js is OK as it can find that file.

So this makes me think that there is something wrong/broken in the configuration of the RSIKPI application (not the miniconda environment) that means at runtime it cannot find anvil.js.window.

But it can in datagridplay.

Can someone nudge me along?

Thanks

Adrian

Spoke too soon, it is still not working.

Both Applications when queried about sys.modules show:

‘anvil.http’: <module ‘http’>,
‘anvil.js.window’: ,
‘anvil.js’: <module ‘js’>,
‘anvil.image’: <module ‘image’>,
‘anvil.media’: <module ‘media’>,

Currently reading about the import system, for Python.

This editor remove the “” from the anvil.js.window entry…

This editor remove the “< Window proxyobject >” from the anvil.js.window entry…

it doesn’t seem like you’re launching your anvil app in the right way. Which guides are you following?

essentially anvil.js.window is only available in client code, and it looks like you’re not running the anvil app in a browser at all.

More context will give the community a better understanding of what’s going on.

Hi,

Yes I am running in a browser, I’ve launched the app server from the miniconda environment I setup:

1 Like

Line 13 is the print of the sys.modules you see in the Anaconda trace.

I’ve done something while crashing about on the learning curve - I am just trying to work out what.

So in the other example DataGridPlay - this works from the same miniconda environment like so:

anvil-app-server --app datagridplay

and for the kpi example:

anvil-app-server --app kpi_tool_for_the_rsa

Both work as expected in the absence of the anvil.js.window work.

thanks for the clarification
Are you able to show your python code for the server function
As well as the client code that calls the function

best to share code using backticks ``` for formatting, rather than screenshots

```python

# server code here

```

If you’re able to share a clone link that would be helpful.

1 Like

I don’t know for sure that this is your issue, but we’ve seen this error when we try to import a client module that includes anvil.js in server-side code.

So we end up with this at the top of our (potentially imported) client modules:

if not anvil.is_server_side():
    import anvil.js

As @stucork is aluding to, you can’t use anvil.js server-side.

Worth noting we’ve been bit by this a BUNCH - it’d be really helpful if therew as a more helpful error message in this case (“You can’t import anvil.js into server code”).

2 Likes

Hi,

Thanks for your input Dan.

I created a stripped down version to ease the distractions in looking at the project.

So Client side code is now:

from ._anvil_designer import RSAKPITemplate
from anvil.js.window import navigator
import anvil.server
import sys

class RSAKPI(RSAKPITemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    print (sys.modules)

    # Any code you write here will run before the form opens.
    self.month.items = [("January", 1),("February",2),("March",3),("April",4),("May",5),("June",6),("July",7),("August",8),("September",9),("October",10),("November",11),("December",12)]
    self.year.items = [("2023", 2023),("2024",2024),("2025",2025)]

  def exit_btn_click(self, **event_args):
    """This method is called when the button is clicked"""
    return

  
  def save_btn_click(self, **event_args):
    """This method is called when the save button is clicked"""
    HC_NEWLINE = '\n'
    HC_TAB = '\t'
    report = 'Hello, where has my anvil.js.code gone?'

    print (report)

    navigator.clipboard.writeText(report)

and the server side:

import anvil.server
import requests
from .RSAKPI import Transport


@anvil.server.callable
def GetRSASIFTER_status(status: int ) -> int:
  return 42

@anvil.server.callable
def GetRSASIFTER_MonthKPI(year: int,month: int, priority: int ) -> Transport.Cargo:
  return 24
  

and as before “Error Report from Client code:”

To create this stripped version, I copied the source on my local workstation then chopped out all the verbose code and then ran in the same mini-conda environment I have set.

I started the page then clicked on “Get data from SIFTER” button which has no event code defined in this stripped example.

I fear I have broken something inside the file structure for the project as it moves when I copy the physical files.

I tried to upload from Github to Anvil but I need a business account for that, and I have not taken out a subscription as yet.

So it looks like RSAKPI is a client side module containing the RSAKPI Form class.
You are importing Transport from the RSKAKPI module.
This is what is breaking on the server.

When you load the RSAKPI module on the server
the server tries to do from anvil.js.window import navigator
But the window object doesn’t exist on the server.
The window object is the browser javascript namespace.

You have a few options.

  • Move Transport somewhere else, likely an isolated client module that doesn’t need the anvil.js.window object.
  • Don’t import navigator directly, instead import anvil.js and replace navigator.clipboard with anvil.js.window.navigator.clipboard
  • do a server side check and only import navigator if you’re not on the server
from anvil import is_server_side
if not is_server_side():
    from anvil.js.window import navigator

I would probably go with the first or third option.

2 Likes

Ahhhhh - thank you.

The error really is server side, I’ll isolate transport, but that server side check is useful too.