To start out, I’m pretty early in my Python and Anvil learning process, so I’m sorry if I’m missing something obvious, or have to ask multiple questions.
I’m trying to build an app that helps my users print PDFs of various layouts (reports) from a system we use called P6. I’ve found a couple packages that automate the keyboard and mouse control, which I’m trying to use to open a layout and then select the print function. The package I’m trying to use is pyautogui.
I’ve set my Anvil app python version to 3.10 (beta) and added pyautogui as a package. That ran successfully. Then, I created server code to use the pyautogui. So far, so good.
When I run it, the Anvil app gives me an error that PyAutoGUI is not installed in the server environment, even though I know it is. So, I did a little research on the forum and ran across this posting: Pls add pyautogui library - #3 by akshit0201
Basically, my conceptual error is understanding how server code works… it’s code running on the server (obvious once I thought about it) and the server doesn’t have a keyboard or mouse that I can automate. And, I can’t install the package to run on the client side.
So… is there a way that I can use Anvil to control and automate a desktop application? This would be used by many people within my company, so I don’t want to ask them to install python… Anvil seemed like the perfect way to distribute this functionality.
Could I freeze some python code into an executable that I save on an internal server, and have Anvil launch the latest/greatest version of that application that I’ve saved on the server? Then, Anvil would just be like a switchboard… is that possible?
You should be able to use Uplink to write a Python program that includes pyautogui and runs on the client machine. Through a client Uplink key it would be able to call server functions on your Anvil app. The Uplink program does not run through a browser, so does not have the same limitations as the Anvil client.
Is this conceptually the same think as using a local jupyter notebook to run code? Or is the Uplink something else?
Probably nobody but me has python installed on their computers. Would the Uplink program be able to run on their computers without python + packages being installed?
Good sleuthing! It is Primavera P6. When I get better at this programming stuff I plan to use the REST API to help feed some data analytics systems and possibly to modify activities/calendars/etc in the schedules. But, as I said, I’m kind of a newbie and not ready to tackle those types of tasks.
I was hoping to start with an “easy” project where the process of creating PDFs from the layouts could be automated. Those PDFs are distributed to the internal project team, our trade partners, and to the owner so we do need PDF format from the desktop P6 client. For some of our large projects, each PDF can be quite time consuming, and if you have 10 or more PDFs to make, a person could easily be chained to their desk watching progress bars for an hour at a time. I wanted to automate this so that the computer is fully occupied doing the churn work, and the person is free to go for a jobsite walk or lunch or whatever.
I have an app that does the same. Here are the moving parts:
Excel or CAD add-ins or various scripts call an http endpoint of the app with the instructions about what to do
The http endpoint adds one row to a data table, launches the background task StartProcess and immediately returns the control to the caller
The StartProcess background task calls the ExecuteProcess function running on an uplink script located on a local server. This local server has Excel, CAD and tons of other software installed and configured
The uplink ExecuteProcess function searches all the rows from the data table with status=="pending", gets the newest, changes the status to "running", does the job, then changes the status to "done"
After completing one process, it restarts with the next pending row if exists, or exits and sends an email summary to the user that requested the tasks
The app has one form that shows the list of rows with status!=“done” and has buttons that allow to push one task at the top of the line or cancel it. The list is refreshed by a timer every 2 seconds
The app is much more complex than this because I have 3 uplink servers and I need to make sure that:
Incoming requests are immediately executed, unless all the servers are already busy
The same server never executes two tasks at the same time
When a server is down, the app should just switch to the next one
When there is a new server (like when I’m debugging from my pc), the app automatically finds it
You sound like a beginner, so this setup may be too complex for you. I described it hoping that gives you an idea of how this can be done.
If I understand you are talking about setting up the open source server on your machine. That part would make all of the above more complex. In fact here you are asking for help about the setup . You can use a local server or the Anvil server, it doesn’t matter. All you need is an uplink script, which acts as a server waiting for the Anvil server to ask for the execution of a function. Setting up an uplink script is easier than setting up an Anvil server. Much easier.
You can have one local machine with a python script running on whatever version of python you like with whatever environment you like. It doesn’t need to be compatible with any Anvil server because you don’t need an Anvil server. Then you can send the requests either from a form or from a call to an http endpoint, then the server (Anvil or yours) will call the uplink script, which is running on one er even more than one dedicated machines.
This answer is very broad because your question is very broad. If you need details, we can go over the details.
Some of the tasks executed by my Queue Manager app are automatic generation of drawings. One task can spend 6 hours creating a thousand drawings.
Then we need to create the PDF. Printing a 1000 drawings to 1000 PDF files, then creating a 1000-page PDF file can take an hour or two.
In my case I cannot have two concurrent processes, so I had to make sure that when 4 requests arrive at the same time, the first 3 are distributed to the 3 available servers and the 4th is left there, sitting for the next server available. This part added most of the complexity to the app.
See the PyInstaller link in my forum post. PyInstaller (and the like) are often discussed on various blogs, Reddit, Stack Overflow, etc. A Google search will turn up a lot of related “how-tos”.
Bear in mind, most of these tools do not fully protect your Python source code. A curious user will be able to read it. Any “sensitive” code should reside server-side.
Getting it work at all will be a major step for a beginner.
Getting it to work reliably, whilst also being maintainable (both of which are absolute requirements for anything your business relies on) will be even harder.
Full Disclosure: I would say this. I’m a freelancer. I make a living doing this stuff for companies.
Well, the Queue Manager is my own app. It is very entangled with other apps, and I can’t give you a clone link.
It was one of the first apps I created with Anvil years ago. I created before background tasks were available, and I went through scary acrobatics to be able to keep tasks running for hours. Then a colleague added a few features, then I rewrote most of it, but skeletons of those acrobatics are still there. It manages many kinds of processes, each with its own quirks. And once in a while they stop working because Microsoft pushes updates that change how software interact with each other.
Even if I gave it to you, you would just get scared. I get scared every time I look at it.
If you want, send me a PM, we can have a talk so I can tell you what to do and what not to do.
As @owen.campbell says, this is not beginner’s stuff, but if you are happy with a few hiccups and with it requiring some manual monitoring, you should be able to get something better than what you have today. For me the best improvement was that we don’t need any longer to keep the perfect configuration for printers, folders, temp files, add-ins, scripts, etc on all machines. Now you request for a task and the 3 servers are well configured and will do the job. And when they get stuck I reboot them and they usually pick up from where they left off.