Python Firebase 模块

2024年8月29日 | 23 分钟阅读

Firebase 是 Python 提供的一个库,用于使用 Firebase 提供的各种服务。为了更好地理解 Firebase 库,我们首先需要了解 Firebase 本身以及它提供的不同服务。Google 的 Firebase 技术允许开发者创建移动和 Web 应用程序。它最初是一家独立公司,成立于 2011 年。Google 于 2014 年收购了该平台,现在它是 Google 应用开发的主要产品。

你可能知道,Firebase 是 Google 用于加速应用开发的工具。它提供 BaaS,即后端即服务,这意味着 Firebase 会处理云基础设施和所有后端需求。这让你能更快地设计和部署应用。实时数据库、Cloud Firestore 和身份验证只是 Firebase 众多强大功能中的一部分。它还支持托管,并为机器学习任务(如文字识别、图片标记等)提供 API!Firebase 是一套工具,可让你“构建、改进和发展你的应用”,它提供的工具涵盖了许多开发者通常需要自己构建但又不想构建的服务,因为他们更愿意专注于应用体验。这包括分析、身份验证、数据库、配置、文件存储、推送消息等。这些服务基于云,并且几乎不需要开发者付出任何努力即可扩展。

当我说“托管在云端”时,我指的是 Google 的后端组件,它们完全由 Google 维护和管理。Firebase 的客户端 SDK 直接与这些后端服务交互,从而消除了你的应用和服务之间的任何中间件需求。如果你正在使用 Firebase 的数据库选项之一,你通常会在客户端应用中编写代码来查询数据库。然而,传统的应用开发通常涉及编写前端和后端代码。前端代码只调用后端提供的 API 端点,由后端代码处理繁重的工作。而使用 Firebase 产品,则绕过了传统的后端,将工作重心放在了客户端。Firebase 控制台为每个产品提供了管理访问权限。

在这些 SDK 之上,还有一个名为 FirebaseUI (Android, iOS, web) 的库,它包含许多有用的工具,使 Firebase 编程变得更加容易。还有一些额外的项目,它们封装了用于 Angular 的 Web SDK,例如 AngularFire。这些都是免费使用的。Firebase 是开源的坚定支持者。

Firebase 允许开发者专注于创造卓越的用户体验。你不需要管理任何服务器,也不需要编写 API。Firebase 就是你的服务器、API 和数据存储,它的设计方式允许你根据特定需求进行定制。是的,对于复杂的应用程序,你偶尔需要利用 Google Cloud 的其他部分。Firebase 不可能对每个人都做到尽善尽美,但它已经非常接近了。

现在,让我们来看一下 Firebase 套件提供的不同服务或产品,所有这些服务或产品都是 Google 提供的 Firebase 套件的一部分。各种服务如下:

  • 实时数据库 (Realtime Database):Firebase 实时数据库是一个托管在云端的数据库。数据以 JSON 格式保存,并在所有连接的客户端之间实时同步。大多数数据库需要 HTTP 请求来获取和同步数据。大多数数据库只在你明确请求时才提供信息。当你将应用连接到 Firebase 时,你使用的不是标准的 HTTP,而是 WebSocket 连接。WebSocket 比 HTTP 快得多。你不需要进行多次 WebSocket 调用,因为单个套接字连接就足够了。所有数据都会在单个 WebSocket 上以客户端网络所能处理的最快速度自动同步。一旦有新数据可用,Firebase 就会立即提供给你。当你的客户端保存数据更改时,更新后的数据几乎会立即发送给所有连接的客户端。
  • 文件存储 (File Storage):Firebase Storage 允许你将二进制文件(最常见的是照片,但任何文件都可以)直接从客户端保存到 Google Cloud Storage!!!为了保护你的 GCloud 存储桶免受大众访问,Firebase Storage 有自己的一套安全规则,同时允许对授权客户进行全面的写入操作。开发者使用 Firebase Cloud Storage SDK 直接从客户端上传和获取文件。如果网络连接不佳,客户端可以从中断处恢复,为你的消费者节省时间和带宽。Cloud Storage for Firebase 将你的文件保存在 Google Cloud Storage 存储桶中,允许你从 Firebase 和 Google Cloud 访问它们。Firebase Cloud Storage SDK 让你能够自由地从移动客户端上传和下载文件。你还可以使用 Google Cloud Storage API 进行服务器端处理,如图像过滤和视频转码。由于 Cloud Storage 会自动扩展,因此无需切换到其他提供商。了解更多关于我们与 Google Cloud 集成的优势。
  • 身份验证 (Authentication):Firebase Auth 包含一个内置的电子邮件/密码身份验证系统。它还通过 OAuth2 支持 Google、Facebook、Twitter 和 GitHub。在大多数情况下,我们将专注于电子邮件/密码身份验证。Firebase 中的 OAuth2 协议有详细的文档,大部分是复制粘贴即可。如果你曾经构建过身份验证系统,让我们先来感叹一下。自定义身份验证完全是一场灾难。我这辈子再也不会写另一个身份验证系统了。我对 Firebase Auth 一见钟情,这种爱从未消退。我偶尔会感到沮丧,我们偶尔会有分歧,但我永远不会忘记自定义认证系统那个冰冷、黑暗的深坑。我细数我的福分。你可以使用 Firebase Auth 来管理对数据的访问,因为它直接与 Firebase 数据库集成。我这样写好像是事后才想到的,但事实并非如此。这是你会喜欢 Firebase Auth 的第二个原因。
  • 托管 (Hosting):Firebase Hosting 是一项对开发者友好的网络内容托管服务。你只需一个命令就可以快速部署 Web 应用,并将静态和动态内容提供给全球 CDN(内容分发网络)。使用 Firebase Hosting 与 Cloud Functions 或 Cloud Run,可以在 Firebase 上构建和托管微服务。对于你所有的静态文件,Firebase 提供了一个易于使用的托管解决方案。它使用 HTTP/2 从全球 CDN 传送文件。Firebase Hosting 专为当今的 Web 开发者设计。随着像 Angular 这样的前端 JavaScript 框架和像 Jekyll 这样的静态生成工具的出现,网站和应用程序比以往任何时候都更加强大。无论是一个简单的应用着陆页还是一个大型的渐进式 Web 应用 (PWA),Hosting 都能为你提供启动和管理网站和应用所需的基础设施、工具和功能。你可以使用 Firebase CLI 将本地计算机文件夹中的文件部署到我们的托管服务器。除了提供静态信息外,你还可以使用 Cloud Functions for Firebase 或 Cloud Run 来提供动态内容并在你的网站上托管微服务。所有内容都通过 SSL 连接从我们全球 CDN 最近的边缘服务器提供。
  • Cloud Firestore:Cloud Firestore 是 Firebase 和 Google Cloud 为移动、Web 和服务器开发提供的多功能、可扩展的数据库。与 Firebase 实时数据库一样,它使用实时侦听器来保持客户端应用之间的数据同步,并为移动和 Web 提供离线支持,让你能够创建响应迅速的应用,无论网络延迟或互联网访问情况如何都能运行。Cloud Firestore 还能与其他 Firebase 和 Google Cloud 技术(如 Cloud Functions)无缝集成。Cloud Firestore 是一个存储在云端的 NoSQL 数据库,你的 iOS、Android 和 Web 应用可以通过原生 SDK 直接访问。除了 REST 和 RPC API,Cloud Firestore 还提供原生 Node.js、Java、Python、Unity、C++ 和 Go SDK。根据 Cloud Firestore 的 NoSQL 数据范式,你将数据存储在文档中,文档中包含映射到值的字段。这些文档保存在集合中,集合是文档的容器,你可以用它来组织数据和构建查询。文档支持从简单的字符和整数到复杂的嵌套结构的各种数据类型。你还可以在文档中创建子集合来设计分层数据结构,随着数据库的增长而扩展。Cloud Firestore 数据模型接受你的应用所需的任何数据结构。

代码

输出

These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
1
Enter the Firebase Database Access link::
https://xxxxxx-yyyyy.firebaseio.com
A connection to the Database was established successfully.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
2
Enter the data you want to add to the Firebase Database::
Enter the name of the student:
Nirnay
Enter the roll no of the student:
35
Enter the marks of the student:
92
{'student_marks': '92', 'student_name': 'Nirnay', 'student_rollno': '35'}
Data added sucessfully to the Firebase Database.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
2
Enter the data you want to add to the Firebase Database::
Enter the name of the student:
Rahul
Enter the roll no of the student:
21
Enter the marks of the student:
97
{'student_marks': '97', 'student_name': 'Rahul', 'student_rollno': '21'}
Data added sucessfully to the Firebase Database.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
2
Enter the data you want to add to the Firebase Database::
Enter the name of the student:
Priyanka
Enter the roll no of the student:
15
Enter the marks of the student:
89
{'student_marks': '89', 'student_name': 'Priyanka', 'student_rollno': '15'}
Data added sucessfully to the Firebase Database.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
3

Data present in the Firebase Database::
'8e606c6eca3711ec' : {'student_marks': '92', 'student_name': 'Nirnay', 'student_rollno': '35'}
'8e606c6eca3711fr' : {'student_marks': '97', 'student_name': 'Rahul', 'student_rollno': '21'}
'8e606c6eca3712gq' : {'student_marks': '89', 'student_name': 'Priyanka', 'student_rollno': '15'}
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
5
Enter the ID of the record that you want to update in the Firebase Database::
8e606c6eca3711fr
Enter the what parameter of the record you want to update?
student_marks
Enter the new value of the student_marks, you want to update?
96
Record updated successfully from the Firebase Database.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
3
Data present in the Firebase Database::
'8e606c6eca3711ec' : {'student_marks': '92', 'student_name': 'Nirnay', 'student_rollno': '35'}
'8e606c6eca3711fr' : {'student_marks': '96', 'student_name': 'Rahul', 'student_rollno': '21'}
'8e606c6eca3712gq' : {'student_marks': '89', 'student_name': 'Priyanka', 'student_rollno': '15'}
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
4
Enter the ID of the record that you want to delete from the Firebase Database::
8e606c6eca3712gq
Record deleted successfully from the Firebase Database.
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
3
Data present in the Firebase Database::
'8e606c6eca3711ec' : {'student_marks': '92', 'student_name': 'Nirnay', 'student_rollno': '35'}
'8e606c6eca3711fr' : {'student_marks': '96', 'student_name': 'Rahul', 'student_rollno': '21'}
To continue with code execution, type [y] otherwise [n].
y
These are the possibilities given below; choose any one of them.::
1. To establish a connection to the Firebase Database using the Firebase module of python.
2. To write data to the Firebase Database using the Firebase module of python.
3. To read data from the Firebase Database using the Firebase module of python.
4. To delete data from the Firebase Database using the Firebase module of python.
5. To update a data/record in the Firebase Database using the Firebase module of python.
6. To terminate the code and stop the choices from being displayed.
6

说明

在上面编写的代码中,解释了 Firebase 模块不同函数的用法。在这段代码中,我们借助 Python 的 Firebase 库,在 Firebase 云中创建了一个数据库。代码中执行了各种操作,这些操作包括连接到 Firebase 云数据库,成功连接后,下一步是从用户那里获取输入数据并将其转换为 Python 字典对象,然后将该字典对象写入 Firebase 云数据库,接着读取存储在 Firebase 云数据库中的所有数据。除了读写,我们还可以从 Firebase 云数据库中删除已有的记录,也可以更新已有的记录。然后,编写了 main 函数,用于创建上述类的对象,通过这个创建的对象,我们可以调用该类中编写的各种方法,每个方法都描述了 Firebase 云数据库的特定功能。Firebase 云数据库支持的不同选项或操作包括:在云数据库中创建新记录、从云数据库中删除已有的记录、读取并显示存储在该特定数据库中的所有记录,以及更新 Firebase 云数据库中已存在记录的特定参数。对于所有这些功能,Firebase 的 Python 库都提供了不同的函数,并且我们在上面编写的类中为所有这些功能编写了不同的函数,以便更好地理解它们。

Firebase 在开发者中如此受欢迎是有原因的,其受欢迎的主要原因在于它为开发者提供了大量的优势,帮助他们节省时间,这些时间可以投入到其他任务中,而不是一次又一次地重复造轮子。现在,让我们快速浏览一下 Firebase 库的一些主要优势。

  • 开发速度:Firebase 是一个很好的应用开发替代方案,可以帮助开发者大幅缩短应用的开发上市时间。对于数据库和后端服务的设置与维护,大多数开发者需要访问服务器和主机。要构建应用,你需要一名后端开发者和一名前端开发者。即使对于后端和前端必须在不同阶段协同工作的小型系统也是如此。同时拥有前端和后端开发者可能会导致错误和困难,从而引发应用问题并增加开发成本和复杂性。然而,使用 Firebase 可以让前端开发者跟踪所有任务,并减少完成任务所需的时间。这一切都归功于其即用型的数据存储、身份验证、警报、分析等服务。此外,Firebase 提供了许多即用型服务,使开发者无需编写样板代码、重复造轮子或从头开始创建后端。Firebase 的高质量 SDK、详尽的文档和活跃的社区将使前端开发者能够轻松地将其集成到他们的代码库中。
  • 由 Google 提供支持:Google 是技术领域最知名、最值得信赖的品牌之一,它为 Firebase 提供支持。自被收购以来,Firebase 经历了一系列的调整和进步,并发展成为今天这个可靠的平台。它利用了 Google Cloud 和多项 Google 服务。Firebase 现在是 Google Cloud Platform 的一部分,它与各种第三方服务连接,并与其他 Google Cloud 服务完美协调运作。自收购以来,Google 一直在 Firebase 上进行大量投资,从而使其产品更加强大。
  • 免费入门:Firebase 是一项免费服务,允许用户使用他们的 Google 帐户登录。Spark 计划是免费的,包含多种功能,可帮助开发者入门。然后,他们可以根据不断变化的需求选择 Blaze 计划。Firebase 能够免费入门是一个很棒的特性,也是它如此受欢迎的原因之一。Spark 计划的数据库读写限制相当宽松。另一个好处是,Blaze 计划会考虑初始的免费限制。假设你将在 Blaze 计划上托管 20GB 的存储空间。由于免费限额是 10GB,Firebase 只会向你收取额外的 10GB 费用。
  • 服务多样性:Firebase 还提供广泛的产品来帮助开发者完成工作。Firestore 和 Firebase 的实时数据库是两种数据库选择。同样,Firebase 的集成 Cloud Functions 支持无服务器应用开发,并允许轻松进行云媒体存储。Firebase 平台包含构建、发布和监控应用的功能,涵盖了整个应用开发周期。它还提供了吸引消费者并让他们在开发周期的最后阶段继续使用应用的方法。
  • 无服务器架构:服务器的扩缩容并不容易!特别是扩展数据库集群很困难,并且为大型工作负载提高性能需要经验丰富的专家。Firebase 通过提供一个完全无服务器的环境解决了这个难题。Firebase 是一个无服务器设计,这意味着客户只需为请求付费,而不必维护或担心服务器基础设施。这与需要始终运行的传统服务器不同。因为 Firebase 是一个无服务器平台,用户只在服务器使用时才付费。因此,效率的提高减少了对可扩展性的担忧。采用亲力亲为的方式处理 DevOps、设置、基础设施和容量规划,为开发者提供了更大的自由度。
  • 对前端友好:Firebase 深受全球开发者的青睐,因为它让他们能够专注于为移动应用编写前端代码。Firebase 减少了对样板后端代码的需求,从而可以更快地开发应用。Firebase 简化了应用开发,同时也降低了成本。开发者和企业还可以使用 Firebase,通过单一、易于学习的技术来标准化后端环境。一个后端模式可以减少支持它所需的培训量,并允许前端开发者处理大部分工作。
  • 用于机器学习:机器学习正处于彻底改变信息技术领域的边缘。它已经以多种方式开始了。根据 Gartner 的说法,30% 的公司将在其运营的至少一个方面使用机器学习。Firebase 也很有用,因为它允许开发者使用机器学习。无论是 iOS 还是 Android 开发者,无论其经验水平如何,都可以使用机器学习。Firebase 配备了一个 ML 工具包,其中包含用于文本识别、人脸检测、图像标记、条形码扫描等移动平台功能的 API。开发者可以根据其应用开发需求,在云端 API 和设备端 API 之间进行选择。
  • 轻松监控错误:Crashlytics 是 Firebase 的一个组成部分,它是一个快速识别和解决问题的绝佳工具。Firebase 可以跟踪非致命和致命错误,并根据故障对用户体验的影响提供报告。
  • 更好的 SEO:通过在 Google 搜索中提供应用链接,Firebase 促进了应用索引,让消费者能够通过 Google 搜索再次参与。通过索引应用,可以提高你的应用在搜索中的排名,让新用户发现并安装它。Android Instant App 可以用来访问程序内容,是开发者的一个好选择。
  • 轻松备份:借助定期备份,Firebase 提供了最高级别的数据保护和可用性。通过依赖该平台的自动备份功能,应用程序可以免受任何潜在的数据丢失。对于 Blaze 计划的用户,可以轻松配置实时数据库进行自动备份。通过单击 Firebase 数据库的“备份”选项卡可以访问自动备份设置。

Firebase 的缺点

  • 复杂查询:用户在使用 NoSQL 数据库时面临的另一个问题是实现复杂查询。尽管 Firebase 的 Cloud Firestore 相对于 RTDB 有了显著改进,但执行复杂查询对某些客户来说仍然是一个障碍。
  • 仅支持 NoSQL:Firebase 的任何数据库选项,无论是 Firestore 还是 Firebase 实时数据库,都无法使用关系型数据库。
  • 国家/地区限制:Google Firebase 的官方网站是 https://firebase.google.com,它在一些国家/地区受到限制。由于中国禁止了带有 *.google.com 和 *.googleapis.com 的 URI,因此 Google 以及其他 Google 服务在中国被屏蔽且无法使用。
  • 没有社区:由于 Firebase 不是一个用于移动应用开发的开源平台,因此对于许多开发者来说,它是一个糟糕的选择。因为 Firebase 是闭源的,用户无法编辑其源代码。由于这个限制,许多喜欢 Firebase 功能的开发者最终会使用其他平台。尽管该平台不是开源的,但在 GitHub 上可以找到一些库和 SDK。这可能是 Firebase 最重要的限制,它阻碍了社区开发产品、提高灵活性水平,并为无法支付 Firebase 费用的开发者提供自托管选项。

因此,在本文中,我们了解了 Python 中 Firebase 模块的用法。


下一个主题儿童 Python 编程