Run server-side code independent of an Anvil app

You can do a form with a button that calls a server function.

You can do a background task and schedule it to run at a certain time, (or launch it from the server function called by that button.)

Or, I think this is the best solution, you can create an uplink script that connects to the app and sees the database as if it were in the server, but runs on your computer.

You can do all of the above with the free tier, with the limitation of max 30 seconds for server functions and background tasks, no time limitations on uplink scripts.

1 Like

@stefano.menci Thank you for the rapid reply and various options. I’ll look into each. Thank you. :blush:

1 Like

@stefano.menci

Indeed, the Server Uplink script approach worked best.

Let me contribute the below code, as it may help others.

Once you create an App-Specific SERVER-SIDE Uplink Key, you can substitute it into the below script; then run that script using the ptpython(1) interpreter to obtain a remote interactive REPL:

user@pyvenv$ ptpython -i ./server_uplink_REPL.py

# =====================================================
# This script name: ./server_uplink_REPL.py
# =====================================================
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server
import anvil.users
# =====================================================

# =====================================================
# Key generated for a specific Anvil Works App.
# =====================================================
SERVER_SIDE_UPLINK_KEY = 'server_*********'
anvil.server.connect(SERVER_SIDE_UPLINK_KEY)
# =====================================================

# =====================================================
# Now, live-interact with that App's resources by using
# the ptpython(1) interactive REPL as follows:
# =====================================================
# nmvega@pyvenv$ ptpython -i ./server_uplink_REPL.py
# =====================================================

This can also help with learning various APIs more quickly.
I hope it helps others.

3 Likes

Thanks for the suggestion.

You mention the very useful interactive REPL using the uplink, and I will add that there are 2 more REPLs that come with Anvil:

  • The app console, available while the app is running, that shares the same interpreter used by the client. For example you can get the main form and its content with get_open_form().contents.get_components(). You can even do alert('Hello!').
  • The server console, I think not available on the free tier. You can open more server consoles. Each starts its own interpreter and loads the code you are editing. Each console is a brand new instance, so you need to import your modules. I sometimes use it for quick exploration of data tables or to fill default values on columns added to the tables, or to import and test some classes I’m working on.
3 Likes

@stefano.menci Thank you for the additional REPL console tips. I just now upgraded to the Personal plan. You’re right that server consoles require a non-free plan. I’ll try those also as I go along.

One reason I love ptpython(1) is because of its vim(1) key-bindings, which I’m used to after decades of using vim(1). The fonts are also bigger:

And now that you mention vim, have a look at this, I bet you will enjoy it: Neovim in the browser

I tried to configure it in Windows, and failed miserably.

Yesterday I got a new computer and I’m going through all the installations and configurations right now. I might give it another try using wsl2…

1 Like

ptpython looks like it might be nice to build into amoni

Thanks for that. I’ve not seen it before.

1 Like

@owen.campbell @stefano.menci

I’m still getting more familiar with Anvil Works, but thank you for bringing the amoni and Neovim project to my attention. amoni looks simple and elegant, and I’ll give it a whirl when ready.

Regarding ptpython, you’ll both love it. Being a CLI person, it’s been part of my daily-driver for many years: Fedora (O/S) + terminator(1) (splittable terminal emulator) + tmux(1) (terminal multiplexer like screen(1)) + ptpython (REPL) + podman(1) (Docker without the daemon) + (etc).

I mostly live inside booted Fedora, but when I boot to Windows-11, I have up-to-date WSL2 running there as well (with Fedora-36 LoL). The reason I mention this is because, up until a few months ago, I was X-Displaying my favorite terminal (terminator(1)) onto the Window desktop. But, then I recently found Fluent Terminal (free) in the Windows App Store, which is an amazing, overdue and highly configurable terminal emulator for Windows. X-Displaying my terminal is no longer necessary. You can configure Fluent Terminal to launch right into your WSL2 Linux distro (Fedora in my case). I’ve been meaning to get vsCode to launch Fluent Terminal when I debug a script, but I haven’t booted into Windows in a while to get to that.

Finally, I’ll definitely try Neovim in the browser. Thank you for bringing it to my attention. If I get it working (not today but soon), I’ll let you guys know.

Thank you again for everything!

1 Like

Well, if we’re sharing some terminal love…

I run ubuntu with Regolith desktop, Xonsh as my shell (so no need for a repl), Kitty as my terminal emulator (I previously used something else plus tmux but not any more) and Neovim as my editor.

My dotfiles are at Github.

(and, yep, I’ve built my my own keyboard too. I’m that much of a cliché).

I very nearly offered to give a talk on this stuff at PyCon UK but, it turns out that I am far too lazy to put it together.

3 Likes

@owen.campbell

xonsh – I just died and went to heaven, I mean just I dnf installed xonsh.

How many times have I wanted to mix & match Bash and Python interactively on the CLI. This is great.

I just have to get vi(1) mode going on its CLI (before anything else due to muscle memory), and then discover everything else later. Naturally, I tried set -o vi which failed. LoL

Is there a quick way to enable vi(1) mode on the CLI?

Also, do things in ones ~/.bash_profile need to be re-implemented somewhere else for xonsh?

Thank you.

2 Likes

Xonsh is indeed a thing of beauty.

I don’t know of vi mode, but now I want it!

And yes, the config goes elsewhere

1 Like
1 Like

@owen.campbell

Just a bit of follow-up trivia and credit where it’s due… :blush:

On the xonsh settings URL you referred me to, the check-mark indicating that the Syntax Highlighting feature is supported via the prompt-toolkit Python library; well that prompt-toolkit library was years-ago written by Jonathan Slenders (very nice guy), and that’s what actually drives the rich interactivity of ptpython(1), also written by Jonathan.

It all ties together. :slight_smile:

1 Like

PS: The pt in ptpython stands for prompt-toolkit.

1 Like

I’ve not heard of xonsh or Regolith before. Thanks for that - downloading both now …

Now, those are a couple of rabbit holes from which you may never emerge!

2 Likes

image

4 Likes

Haha. I looked at xonsh, loved it, but as soon as I saw the initial work that it might take, I had to hit pause. For example, I was foolishly hoping that my ~.profile would somehow just work. Don’t laugh all at once. LoL. :stuck_out_tongue_closed_eyes: Plus, I’m still getting up to speed on this platform and other items.

I would love to know more about your love affairs.

Let us know if when your talk is ready. I’m far too lazy to dig through all that material by myself :crossed_fingers:

2 Likes

I tried xonsh, and I loved the few things I could do with it. But I work on Windows, where some of the shortcuts don’t work and if you press the wrong key, for example any arrow key, the terminal gets corrupted and, if you keep working without seeing what you do, it crashes after a few commands.

I first failed to configure neovim, now xonsh.

Linux is creeping more and more inside Windows with wsl2, but my Python environments interact with Excel and other software via COM, so I can’t jump the fence.

Oh well…

1 Like