Mongoengine. mongodb integration

Hi, I am trying to build a back-end administration interface, and I was wondering if it’s possible for Anvil to integrate with a current python, pyramid and mongodb web application for use as the back-end admin?
Kind regards,
Roger

You can certainly use the pymongo library in Anvil (https://pypi.org/project/pymongo/3.7.1/)

Just import it into your server module.

1 Like

Hi @rog911turbo, welcome to the Forum!

Have you heard about the Uplink? It allows you to connect your Anvil app to Python running on any machine.

The Anvil Uplink

You can find it under Uplink in the Gear Menu:

57

When you enable it, your app has a unique key such as AAXSATAAC3LOHHUOEF2GNV3Q-JASZP25IW53ZGSTQ that you use to connect.

You must install the Anvil Uplink library:

pip install anvil-uplink

Then connect by running these lines:

import anvil.server

anvil.server.connect("<Your key here>")

So you could add these lines to your Pyramid app to connect it to Anvil.

Calling functions in your Pyramid app from Anvil

The Uplink allows you to decorate functions as callable. So in your Pyramid app, you could have

@anvil.server.callable
def add_product(name):
    return catalog.add(name)

And you would be able to call add_product from Anvil:

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('add_product', self.text_box_1.text)

You can call add_product from either client or server code.

(I’m not familiar with Pyramid so this is a generic example!)

Calling functions in Anvil from your Pyramid app

The Uplink works both ways. So you could create a function in a server module and call it from your Pyramid app:

# In an Anvil Server Module
@anvil.server.callable
def send_welcome_email(to_address):
  anvil.email.send(to=to_address, text="Welcome to my Pyramid app!")

Your Pyramid app just calls it in the same way the Anvil client code would:

# In your Pyramid app
def welcome_the_user(user_email):
  anvil.server.call('send_welcome_email', to_address=user_email)

HTTP is another option

You can make HTTP requests from Anvil apps, and you can decorate server functions to turn them into HTTP endpoints (just like in Flask). So if you wanted to make your Anvil app interface with your Pyramid app using HTTP, that’s an option too.

I would go with the Uplink as a first choice, but HTTP is there if you need it!

Connecting directly to your MongoDB instance

As @david.wylie mentions, pymongo is available in Server Modules if you want to connect directly to your database.

We can install any other Python packages you want, just ask here or email support@anvil.works.

Does that work for you?

Let me know if that helps or if you’d like more detail :slight_smile:

2 Likes