在瓶的Web API [英] Web API in Flask

查看:157
本文介绍了在瓶的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用瓶创建我的应用程序的Web API,但是有一些问题让我烧瓶的应用程序知道我的其他对象。

I want to use Flask to create a web API for my application, but have some problems to make my flask app aware of my other objects.


  • 我想用瓶,以便能够通过HTTP请求我的应用程序进行交互。所以在我的情况下,整个瓶应用的只是一个外部API ,并依赖于一个核心的应用程序。

  • 让我们想象一下,我的烧瓶应用程序将有执行数据库
    来电。

  • 要在我的应用程序管理数据库调用,我使用的单个对象连接到数据库ANS 实现某种队列

  • 这意味着我在后台的运行核心应用程序,以使数据库调用有提及我的db对象

  • 这是由这个核心应用给予我的队列对象的引用完成。

  • 现在,我希望能够执行使用烧瓶申请过的数据库操作。

  • I want to use Flask in order to be able to interact with my application through http requests. So the whole flask application in my case is just an external API, and relies on a core application.
  • Let's imagine that my flask application will have to perform database calls.
  • To manage database calls in my application, I use a single object that connects to the db ans implements some kind of Queue.
  • That means my core application running in the background has a reference to my db object in order to make db calls.
  • This is done by giving a reference to my queue object to this core application.
  • Now I want to be able to perform actions on the db using a flask application too.

什么是到此队列对象的引用传递给我的应用程序瓶的正确方法是什么?

What is the correct way to pass a reference to this Queue object to my Flask application?

如果我在模块级别定义我的所有对象,我也没办法后与他们进行互动,是吧?

If I define all my objects at module level, I have no way to interact with them afterwards, do I?

合剂应用程序的所有例子中使用瓶作为系统的核心,并定义模块级,在其应用的一切。怎样使瓶只是我的应用程序的一部分?
谢谢!

All the example of Flask applications use Flask as the core of their system and define everything in their app on module level. How do I make Flask just a part of my app? Thx!

推荐答案

我不知道你所说的

如果我在模块级别定义我的所有对象,我也没办法后与他们进行互动,是吧?

If I define all my objects at module level, I have no way to interact with them afterwards, do I?

但是,没有,你没有在模块级定义你的对象 - 这是你的实例,蓝图和您所提供的任何对象都是如此。例如,您可以创建一个的AppBuilder 类,它并配置实例。

But no, you don't have to define your objects at the module level - that's true of your Flask instance, blueprints and any object which you provide. For example you can create an AppBuilder class that makes and configures Flask instances.

对于一些互动背景当地人是一个非常方便的工具为好。

For some interactions context locals are a very handy tool as well.

如果你能澄清这个问题我会尝试扩大我的回答。

If you can clarify the issue I'll try to expand my answer.

这篇关于在瓶的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆