Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent CLI from loading on import of robyn module #801

Open
LilyRose2798 opened this issue Apr 21, 2024 · 4 comments
Open

Prevent CLI from loading on import of robyn module #801

LilyRose2798 opened this issue Apr 21, 2024 · 4 comments
Labels
enhancement New feature or request summer

Comments

@LilyRose2798
Copy link

It currently looks to be impossible to implement your own argument parser when using Robyn, as just importing the robyn module creates its own ArgumentParser. I do not think that simply importing a module should have this behavior, and that you should have to pass a flag to the Robyn constructor in order for it to create the ArgumentParser instance. You should also be able to create your own Config object containing all the values like processes, workers, etc when initializing Robyn, so that if you have your own args or other way of getting these values, then you can pass them through. I'm currently having to do this in the following way:

app = Robyn(__file__, config=argparse.Namespace(**{
    "processes": 1,
    "workers": 1,
    "dev": False,
    "create": False,
    "docs": False,
    "open_browser": False,
    "version": False,
    "compile_rust_path": None,
    "create_rust_file": None,
    "log_level": "INFO",
}))

So a better way of doing this would be desirable.

@LilyRose2798 LilyRose2798 added the enhancement New feature or request label Apr 21, 2024
@sansyrox
Copy link
Member

Hey @LilyRose2798 👋

Thanks for creating this issues and bringing this to my attention.

It currently looks to be impossible to implement your own argument parser when using Robyn, as just importing the robyn module creates its own ArgumentParser

Let me dive a bit deeper and come back with an approach to tackle this.

@Mr-Sunglasses
Copy link
Contributor

Hey @LilyRose2798 👋

Thanks for creating this issues and bringing this to my attention.

It currently looks to be impossible to implement your own argument parser when using Robyn, as just importing the robyn module creates its own ArgumentParser

Let me dive a bit deeper and come back with an approach to tackle this.

IMO, The one way we can tackle this is to rewrite Robyn whole CLI api, where we expose the functions like workers, debug etc. to the Robyn() class and the seprate logic for CLI. Something similar to uvicorn or flask

@LilyRose2798
Copy link
Author

LilyRose2798 commented Apr 23, 2024

A way around the issue I found that isn't ideal is to move the Robyn import to after I've declared my own ArgumentParser:

from argparse import ArgumentParser, Namespace

parser = ArgumentParser("myapi")
parser.add_argument("-a", "--address", default = "127.0.0.1", help = "the address to use for the api (default: %(default)s)")
parser.add_argument("-p", "--port", default = 8000, type = int, help = "the port to use for the api (default: %(default)s)")
parser.add_argument("-l", "--limit", default = 3600, type = int, help = "the number of requests per hour to limit usage of the api to (default: %(default)s)")
parser.add_argument("--processes", default = 1, type = int, help = "the number of processes to use for the api (default: %(default)s)")
parser.add_argument("--workers", default = 1, type = int, help = "the number of workers to use for the api (default: %(default)s)")
parser.add_argument("--log-level", default = "INFO", help = "the log level use for the api (default: %(default)s)")
args = parser.parse_args()

from robyn import Robyn

app = Robyn(__file__, Namespace(**{
    "processes": args.processes,
    "workers": args.workers,
    "dev": False,
    "create": False,
    "docs": False,
    "open_browser": False,
    "version": False,
    "compile_rust_path": None,
    "create_rust_file": None,
    "log_level": args.log_level,
}))

# API routes declared here...

app.start(host = args.address, port = args.port)

@sansyrox sansyrox added the summer label Jun 1, 2024
@sansyrox
Copy link
Member

sansyrox commented Jun 9, 2024

@LilyRose2798 , this seems a bit too complicated. I am thinking of just adding an override args flag and then add the ability the ability to pass through the app.start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request summer
Projects
None yet
Development

No branches or pull requests

3 participants