Detecting whether anvil is running in the browser (typing)

I do this for the forms:

if anvil.server.context.type != 'browser':
    # running on server
    from typing import List, Optional, Union, Tuple, Dict

And this for the server modules:

if anvil.server.context.type == 'uplink':
    # running tests on pc
    from client_code.Status import Status
else:
    # running on Anvil server
    from .Status import Status

EDIT
I just realized that you use the comments for type hinting with PyCharm.

You don’t need to use comments, you can use the standard Python syntax.

Skulpt doesn’t import typing, but doesn’t do anything with the type hints, perhaps the parser just ignores them. I just checked one of my apps and these are two lines from a module imported both on the client and on the server side:

self._crates: Dict[str, Crate] = {}
self._truck_versions: Optional[List[Project]] = None
3 Likes