Anvil SDK for PyCharm

What I want
I would like a package to import in PyCharm and simulate the Anvil environment, something like the Google App Engine SDK.

Why I want it

  • The Anvil IDE is great for developing simple stuff, but the lack of debugger and advanced tools make the development of complex modules very difficult
  • Developing complex modules in PyCharm and copying them into Anvil is easy if they don’t interact with the Anvil tables or other Anvil modules

Use case
I am working on an app that uses the anvil.http module with maps.googleapis.com, the anvil.tables module to store (cache) some of the result of those requests, the anvil.server module to decorate some of the functions.

The logic is getting very complex and I have several classes interacting with each other with complex data structures.

After a few days working with the Anvil IDE I really started missing PyCharm, its debugger, shortcuts, tools, interface, etc. So I created an anvil package (folder named anvil containing a __init__.py file) with 3 modules:

  • tables.py with the class app_tables with the code for simulating the Anvil database
  • http.py with a request() function and a little more
  • server.py with dummy wrappers callable() and http_endpoint()

At this point I can copy the content of any server module from the Anvil IDE and paste it in a module in the PyCharm project and everything works as if it was online.
I can create an unittest suite, a bunch of test datasets for each database table and play around for a week or two, keeping track of the changes with git, using the debugger and all the fancy PyCharm goodies.
When I will be happy with the behavior of my classes I will copy the content of the module from PyCharm and paste it back into the Anvil IDE. At this point polishing the interface and a few last minute things in Anvil will be a breeze.

Obviously the 3 modules that I created are not complete. For example the tables can get, search, modify, delete and add_row, but I can’t manage relations (link-to-table columns). I am adding features as I need them.

6 Likes

Hi Stefano,

If you want to debug Anvil-connected code in PyCharm, I think the Uplink is the “Anvil SDK” you’re looking for. (Tutorial here / Docs here)

Almost any code you can run in an Anvil server module, you can run on an Uplink. You have access to your app’s data tables, the full anvil.http library - the works. And you can run (and debug) uplink code from anywhere that can run Python, including PyCharm.

I highly recommend it!

4 Likes

Thanks, I will try it, it looks like it’s exactly what I need.

(Actually for now I will keep my little mock modules because they allow me to test the application with test data like this: app_tables.inventory = app_tables.Table(test_data=sample_data.inventory1))

I had watched the uplink video on day one, and I thought “that’s cool, I will look into it on my next IOT little project”.

I thought that the uplink was a way for the Anvil server to contact another server, I had not realized that a module running on my computer could directly access the tables and other Anvil services without requiring the creation of a bunch of http_endpoint wrappers.

I feel like I miss two chapters in the documentation:

  • one about debugging that shows how to use the print + app log for beginners and the uplink + PyCharm for advanced users
  • one about the uplink that shows how it can be used as an http_endpoint on steroids
3 Likes

HI Stefano, I like PyCharm too and even though uplink is great, there is no substitute for seeing the green

======================= 53 passed in 5.83 s =============================

like you see, after all your pytests pass running completely on your laptop

I am going a little bit further now and able to test some client_side Forms on my laptop. Like you,
I substituted the anvil folder for server-side tests and the _anvil_designer folder for the client-side tests. pyDALAnvilWorks on GitHub

For the database, I am using a sqlite substitution, wrapping pyDAL (database abstraction layer) with anvil.works commands so I do not have to change any server or client side code.

Its all pretty rough around the edges right now, but its got to the stage that something like the following can run on your laptop without internet:

def test_init(self):
        # set repeating panel items
        user = anvil.users.get_user()

        contact = contact_generator(user). # this is a dictionary
        contact_form = ContactForm(contact=contact).  # this is a form with repeating panels, text_boxes
        # check that initialized ok by reading the forms and setting the new contact
        assert contact['name'] == contact_form.text_box_name.text
        assert contact['phone_list'][0].number == contact_form.repeating_phone_list.items[0]['text']
        contact_form.button_save_click()

3 Likes

Hi Ben, I recently started following these guidelines for test-driven development so the development starts in Anvil with the creation of the app, then it’s 100% on PyCharm until I’m sure all the logic works well and it’s covered by tests, then it’s back online in the Anvil IDE for the UI. This last step is very fast, because the classes are UI-ready and there is no need to create any helper that returns dictionaries for the repeating panels or other tedious work.

I also started using the DataGridJson, which cuts the number of forms down to less than half (when you have multiple DataGrids in many forms) and the code is better organized because all the event handlers are in the form rather than spread in the row template forms.

All the client code in Globals is easily testable because there are no interactions with UI, database or other services. The server code is very little and almost completely tested, because it’s used by Globals. And it’s easy to decide whether to debug it in PyCharm or to run it in the server (where it’s usually faster while interacting with the database).

I abandoned the mock databases classes because (1) I kept finding out that something was missing and (2) I was afraid (actually hoped) that Anvil would improve app_tables and I would keep falling behind (indeed app_tables has improved, there are the new q operators, etc.). Now I test the exact same code that runs in production.

3 Likes

If I’m understanding this all correctly, it’s possible to run client and server code in an environment like VSC or Pycharm, but not get the autocompletion features, because what you’re doing is using the anvil uplink, and not actually getting the definition for the types of the datatables classes or ui elements.

What I, and I think what you want, is access to the types, so we can get access to things like autocomplete without all the headache of doing mocks… etc…

New feature request?

Because I’d really, really like to be able to just use VSC, with autocomplete provided by some kind of Anvil service / extension, and have full integration for autocomplete.

So basically the option to use the Anvil IDE for lighter work, with the option to plug into environments that already do a bunch of heavy lifting.

2 Likes

This is exactly the use case for static typing and higher powered IDEs.

You need more power as the complexity grows over time.

Ihaven’t tried it, but this looks interesting: Anvil IDE Semi-Continuous Integration - #2 by ben.lawrence

2 Likes

I dig it. Just needs to be turned into an Anvil supported IDE extension for Pycharm / VSC, and I might just cry with joy.

2 Likes