Custom recipe: how to invoke from Python?

Hi, let's say I have a file, in which I have this recipe:

def textcat_custom_model(*args):
      # do stuff

Is it possible to call this recipe from Python (instead of command line)?

By calling from python, I mean as textcat_custom_model(*args), i.e. excluding operations equivalent to command line such as or Jupyter magic.

The main reason I would like such usage is so that I can pass Python objects (dynamically generated callables, for instance) as arguments of the recipe.


Hi! I think what you're looking for is the prodigy.serve function: It looks like a command-line command for consistency, but it's not actually calling a command in a subprocess – it gets the wrapped recipe function, parses the arguments, calls the function, uses it to initialize the controller and then starts the Prodigy web server.

Here's how it looks under the hood:

from prodigy import get_recipe
from import server 
import shlex

def serve(command, *recipe_args, **config):
    cli_args = shlex.split(command)
    recipe_name = cli_args.pop(0)
    loaded_recipe = get_recipe(recipe_name)
    controller = loaded_recipe(*cli_args, use_plac=True)
    server(controller, controller.config)

Thank you Ines! It worked.

Here's what I did, for those who might find this post relevant:
(this was using Prodigy 1.8.5, with Python 3.6, on 64-bit Linux)


@prodigy.recipe('custom.textcat' # sparing the args)
def custom_textcat(*args):
    A custom recipe to be called directly in Python.
    # do stuff

In another script that uses this recipe:

# the imports below let prodigy recognize the custom.textcat recipe by name
import prodigy
from custom_recipe import custom_textcat 

args = [] # fill in arguments here
prodigy.serve("custom.textcat", *args)
1 Like