100% found this document useful (1 vote)
25 views

FastAPI: Modern Python Web Development 1st Edition Bill Lubanovic instant download

The document provides information about the book 'FastAPI: Modern Python Web Development' by Bill Lubanovic, which covers building web applications using the FastAPI framework. It highlights the framework's advantages, including speed and ease of use for Python developers, along with practical guides on various topics such as RESTful APIs, data validation, and asynchronous functions. The book aims to equip readers with the knowledge to effectively utilize FastAPI in modern web development.

Uploaded by

kutterjadeja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
25 views

FastAPI: Modern Python Web Development 1st Edition Bill Lubanovic instant download

The document provides information about the book 'FastAPI: Modern Python Web Development' by Bill Lubanovic, which covers building web applications using the FastAPI framework. It highlights the framework's advantages, including speed and ease of use for Python developers, along with practical guides on various topics such as RESTful APIs, data validation, and asynchronous functions. The book aims to equip readers with the knowledge to effectively utilize FastAPI in modern web development.

Uploaded by

kutterjadeja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

FastAPI: Modern Python Web Development 1st

Edition Bill Lubanovic pdf download

https://ebookmeta.com/product/fastapi-modern-python-web-
development-1st-edition-bill-lubanovic/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

FastAPI: Modern Python Web Development (First Early


Release) Bill Lubanovic

https://ebookmeta.com/product/fastapi-modern-python-web-
development-first-early-release-bill-lubanovic/

High-Performance Web Apps with FastAPI: The


Asynchronous Web Framework Based on Modern Python 1st
Edition Malhar Lathkar

https://ebookmeta.com/product/high-performance-web-apps-with-
fastapi-the-asynchronous-web-framework-based-on-modern-
python-1st-edition-malhar-lathkar/

High-Performance Web Apps with FastAPI: The


Asynchronous Web Framework Based on Modern Python 1st
Edition Malhar Lathkar

https://ebookmeta.com/product/high-performance-web-apps-with-
fastapi-the-asynchronous-web-framework-based-on-modern-
python-1st-edition-malhar-lathkar-2/

Problems and Pitfalls in Medical Literature A Practical


Guide for Clinicians Adam L Cohen

https://ebookmeta.com/product/problems-and-pitfalls-in-medical-
literature-a-practical-guide-for-clinicians-adam-l-cohen/
Prelude to Tragedy: The Negotiation and Breakdown of
the Tripartite Convention of London, October 31, 1861
Carl H. Bock

https://ebookmeta.com/product/prelude-to-tragedy-the-negotiation-
and-breakdown-of-the-tripartite-convention-of-london-
october-31-1861-carl-h-bock/

A Critical Cinema 2: Interviews with Independent


Filmmakers Scott Macdonald

https://ebookmeta.com/product/a-critical-cinema-2-interviews-
with-independent-filmmakers-scott-macdonald/

Housing the Future Alternative Approaches for Tomorrow


1st Edition Graham Cairns

https://ebookmeta.com/product/housing-the-future-alternative-
approaches-for-tomorrow-1st-edition-graham-cairns/

Rebel: My Escape from Saudi Arabia to Freedom 1st


Edition Rahaf Mohammed

https://ebookmeta.com/product/rebel-my-escape-from-saudi-arabia-
to-freedom-1st-edition-rahaf-mohammed/

Impacts of Rationing and Missed Nursing Care:


Challenges and Solutions 1st Edition Papastavrou

https://ebookmeta.com/product/impacts-of-rationing-and-missed-
nursing-care-challenges-and-solutions-1st-edition-papastavrou/
Guns for Hire The Modern Adventure Thriller 2012 Barry
Forshaw

https://ebookmeta.com/product/guns-for-hire-the-modern-adventure-
thriller-2012-barry-forshaw/
FastAPI
Modern Python Web Development

Bill Lubanovic
FastAPI
FastAPI is a young yet solid framework that takes advantage
of newer Python features in a clean design. As its name
implies, FastAPI is indeed fast, rivaling similar frameworks “FastAPI made simple!
in languages such as Golang. With this practical book, This book excels at
developers familiar with Python will learn how FastAPI simplifying FastAPI
lets you accomplish more in less time with less code. concepts, showcasing
Author Bill Lubanovic covers the nuts and bolts of FastAPI the author’s mastery.
development with how-to guides on various topics such as Readers will gain
forms, database access, graphics, maps, and more that will practical knowledge
take you beyond the basics. The book also gets you up to
and hit the ground
speed on RESTful APIs, data validation, authorization, and
performance. With its similarities to frameworks like Flask
running.”
and Django, you’ll find it easy to get started with FastAPI. —Ganesh Harke
Senior Software Engineer, Citibank
Through the course of this book, you will:
• Learn how to build web applications with FastAPI
“This book provides
• Understand the differences between FastAPI, Starlette,
and Pydantic a comprehensive
• Learn two features that set FastAPI apart: asynchronous
overview of the FastAPI
functions and data type checking and validation framework and its
• Examine new features of Python 3.8+, especially
surrounding ecosystem,
type annotations giving readers a quick
• Understand the differences between sync and async Python
yet comprehensive
view of modern web
• Learn how to connect with external APIs and services
development.”
Bill Lubanovic has been a developer for over 40 years, —William Jamir Silva
specializing in Linux, the Web, and Python. He recently Senior Software Engineer, Adjust GmbH
used FastAPI with his team to rewrite a large biomedical
research API. Bill coauthored Linux System Administration
and wrote Introducing Python, both for O’Reilly.

WEB DEVELOPMENT Twitter: @oreillymedia


linkedin.com/company/oreilly-media
US $55.99 CAN $69.99
youtube.com/oreillymedia
ISBN: 978-1-098-13550-8
55599

9 781098 135508
FastAPI
Modern Python Web Development

Bill Lubanovic

Beijing Boston Farnham Sebastopol Tokyo


FastAPI
by Bill Lubanovic
Copyright © 2024 Bill Lubanovic. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (https://oreilly.com). For more information, contact our corporate/institu‐
tional sales department: 800-998-9938 or [email protected].

Acquisitions Editor: Amanda Quinn Indexer: BIM Creatives, LLC


Development Editor: Corbin Collins Interior Designer: David Futato
Production Editor: Kristen Brown Cover Designer: Karen Montgomery
Copyeditor: Sharon Wilkey Illustrator: Kate Dullea
Proofreader: Liz Wheeler

November 2023: First Edition

Revision History for the First Edition


2023-11-06: First Release

See http://oreilly.com/catalog/errata.csp?isbn=9781098135508 for release details.

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. FastAPI, the cover image, and related
trade dress are trademarks of O’Reilly Media, Inc.
The views expressed in this work are those of the author and do not represent the publisher’s views. While
the publisher and the author have used good faith efforts to ensure that the information and instructions
contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or
omissions, including without limitation responsibility for damages resulting from the use of or reliance
on this work. Use of the information and instructions contained in this work is at your own risk. If any
code samples or other technology this work contains or describes is subject to open source licenses or the
intellectual property rights of others, it is your responsibility to ensure that your use thereof complies
with such licenses and/or rights.

978-1-098-13550-8
[LSI]
To the loving memory of my wife, Mary, my parents, Bill and Tillie, and my friend, Rich.
I miss you.
Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Part I. What’s New?


1. The Modern Web. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Preview 3
Services and APIs 4
Kinds of APIs 4
HTTP 5
REST(ful) 5
JSON and API Data Formats 7
JSON:API 7
GraphQL 8
Concurrency 8
Layers 9
Data 12
Review 13

2. Modern Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Preview 15
Tools 15
Getting Started 16
Python Itself 17
Package Management 17
Virtual Environments 17
Poetry 18
Source Formatting 19

v
Testing 19
Source Control and Continuous Integration 19
Web Tools 19
APIs and Services 20
Variables Are Names 20
Type Hints 21
Data Structures 21
Web Frameworks 21
Django 22
Flask 22
FastAPI 22
Review 23

Part II. A FastAPI Tour


3. FastAPI Tour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Preview 27
What Is FastAPI? 27
A FastAPI Application 28
HTTP Requests 32
URL Path 33
Query Parameters 34
Body 36
HTTP Header 37
Multiple Request Data 38
Which Method Is Best? 39
HTTP Responses 39
Status Code 39
Headers 40
Response Types 40
Type Conversion 41
Model Types and response_model 42
Automated Documentation 43
Complex Data 47
Review 47

4. Async, Concurrency, and Starlette Tour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49


Preview 49
Starlette 49
Types of Concurrency 50
Distributed and Parallel Computing 50

vi | Table of Contents
Operating System Processes 50
Operating System Threads 51
Green Threads 51
Callbacks 51
Python Generators 52
Python async, await, and asyncio 53
FastAPI and Async 55
Using Starlette Directly 57
Interlude: Cleaning the Clue House 57
Review 59

5. Pydantic, Type Hints, and Models Tour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61


Preview 61
Type Hinting 61
Data Grouping 64
Alternatives 68
A Simple Example 69
Validate Types 72
Validate Values 73
Review 75

6. Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Preview 77
What’s a Dependency? 77
Problems with Dependencies 78
Dependency Injection 78
FastAPI Dependencies 78
Writing a Dependency 79
Dependency Scope 80
Single Path 80
Multiple Paths 81
Global 81
Review 82

7. Framework Comparisons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Preview 83
Flask 83
Path 84
Query Parameter 85
Body 85
Header 86
Django 86

Table of Contents | vii


Other Web Framework Features 87
Databases 88
Recommendations 88
Other Python Web Frameworks 88
Review 89

Part III. Making a Website


8. Web Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Preview 93
Interlude: Top-Down, Bottom-Up, Middle-Out? 94
RESTful API Design 95
File and Directory Site Layout 97
The First Website Code 98
Requests 100
Multiple Routers 102
Build the Web Layer 103
Define Data Models 103
Stub and Fake Data 104
Create Common Functions Through the Stack 104
Create Fake Data 104
Test! 109
Using the FastAPI Automated Test Forms 110
Talking to the Service and Data Layers 112
Pagination and Sorting 113
Review 114

9. Service Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115


Preview 115
Defining a Service 115
Layout 116
Protection 116
Functions 116
Test! 118
Other Service-Level Stuff 120
Logging 120
Metrics, Monitoring, Observability 120
Tracing 120
Other 121
Review 121

viii | Table of Contents


10. Data Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Preview 123
DB-API 123
SQLite 125
Layout 127
Making It Work 127
Test! 131
Full Tests 131
Unit Tests 140
Review 142

11. Authentication and Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


Preview 143
Interlude 1: Do You Need Authentication? 144
Authentication Methods 145
Global Authentication: Shared Secret 145
Simple Individual Authentication 148
Fancier Individual Authentication 149
OAuth2 150
User Model 151
User Data Layer 151
User Fake Data Layer 153
User Service Layer 154
User Web Layer 156
Test! 158
Top Layer 158
Authentication Steps 158
JWT 159
Third-Party Authentication: OIDC 159
Authorization 160
Middleware 161
CORS 162
Third-Party Packages 163
Review 163

12. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


Preview 165
Web API Testing 165
Where to Test 166
What to Test 166
Pytest 167
Layout 168

Table of Contents | ix
Automated Unit Tests 168
Mocking 168
Test Doubles and Fakes 171
Web 173
Service 175
Data 177
Automated Integration Tests 178
The Repository Pattern 179
Automated Full Tests 180
Security Testing 182
Load Testing 182
Review 183

13. Production. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185


Preview 185
Deployment 185
Multiple Workers 186
HTTPS 187
Docker 187
Cloud Services 188
Kubernetes 188
Performance 188
Async 188
Caches 189
Databases, Files, and Memory 189
Queues 189
Python Itself 190
Troubleshooting 190
Kinds of Problems 190
Logging 191
Metrics 191
Review 191

Part IV. A Gallery


14. Databases, Data Science, and a Little AI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Preview 195
Data Storage Alternatives 195
Relational Databases and SQL 196
SQLAlchemy 197
SQLModel 198

x | Table of Contents
SQLite 199
PostgreSQL 199
EdgeDB 199
Nonrelational (NoSQL) Databases 200
Redis 200
MongoDB 200
Cassandra 200
Elasticsearch 201
NoSQL Features in SQL Databases 201
Database Load Testing 201
Data Science and AI 203
Review 205

15. Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207


Preview 207
Multipart Support 207
Uploading Files 207
File() 208
UploadFile 209
Downloading Files 210
FileResponse 210
StreamingResponse 211
Serving Static Files 211
Review 213

16. Forms and Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


Preview 215
Forms 215
Templates 217
Review 220

17. Data Discovery and Visualization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221


Preview 221
Python and Data 221
PSV Text Output 222
csv 222
python-tabulate 223
pandas 224
SQLite Data Source and Web Output 225
Chart/Graph Packages 225
Chart Example 1: Test 226
Chart Example 2: Histogram 228

Table of Contents | xi
Map Packages 229
Map Example 230
Review 232

18. Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233


Preview 233
Python Game Packages 233
Splitting Game Logic 234
Game Design 234
Web Part One: Game Initialization 235
Web Part Two: Game Steps 236
Service Part One: Initialization 238
Service Part Two: Scoring 238
Test! 239
Data: Initialization 240
Let’s Play Cryptonamicon 240
Review 242

A. Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

B. Creatures and Humans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

xii | Table of Contents


Preface

This is a pragmatic introduction to FastAPI—a modern Python web framework. It’s


also a story of how, now and then, the bright and shiny objects that we stumble across
can turn out to be very useful. A silver bullet is nice to have when you encounter a
werewolf. (And you will encounter werewolves later in this book.)
I started programming scientific applications in the mid-1970s. And after I first met
Unix and C on a PDP-11 in 1977, I had a feeling that this Unix thing might catch on.
In the ’80s and early ’90s, the internet was still noncommercial, but already a good
source for free software and technical info. And when a web browser called Mosaic
was distributed on the baby open internet in 1993, I had a feeling that this web thing
might catch on.
When I started my own web development company a few years later, my tools were
the usual suspects at the time: PHP, HTML, and Perl. On a contract job a few years
later, I finally experimented with Python and was surprised at how quickly I was able
to access, manipulate, and display data. In my spare time over two weeks, I was able
to replicate most of a C application that had taken four developers a year to write.
Now I had a feeling that this Python thing might catch on.
After that, most of my work involved Python and its web frameworks, mostly Flask
and Django. I particularly liked the simplicity of Flask and preferred it for many jobs.
But just a few years ago, I spied something glinting in the underbrush: a new Python
web framework called FastAPI, written by Sebastián Ramírez.
As I read his (excellent) documentation, I was impressed by the design and thought
that had gone into it. In particular, his history page showed how much care he had
taken evaluating alternatives. This was not an ego project or a fun experiment, but a
serious framework for real-world development. Now I had a feeling that this FastAPI
thing might catch on.

xiii
Exploring the Variety of Random
Documents with Different Content
Lisardo
Pues
. no me hagas discurrir.
Calab¿Ella
. no es enredadora?
Quien es sé. ¿No es embustera?
Quien es sé. ¿No es bachillera?
Quien es sé. ¿No es habladora?
La misma razon lo enseña
Quien es, sí, jurado á Dios.
Lisardo
Dílo.
.
Calab.Aquí para los dos...
Lisardo
Prosigue.
.
Calab. Es alguna dueña.
Lisardo
¡Qué
. disparate!

ESCENA V.
SILVIA. — Dichos; poco despues DON FÉLIX.

Silvia. Lisardo,
Que aquí me escucheis os pido.
Calab¡Mujer!
. ¿de dónde has caido?
Lisardo
Ya. lo que quieres aguardo.
SilviaUna
. dama, de quien vos
La casa, señor, sabeis,
Que á su ventana llameis
Esta noche os pide. Adios.
(Vase.)
CalabTapada
. de las tapadas,
Oye.
Lisardo
Tente;
. ¿dónde vas?
CalabDeja,
. que no quiero más
De darla dos bofetadas,
Que las lleve á su señora...
Lisardo
¿Hay
. quién tus locuras crea?
CalabPorque
. otra vez no me sea
Dueña engerta.
Lisardo. Escucha agora:
Pues que ya la noche fria,
En mal distinto arrebol,
Da priesa diciendo al sol
Que se vaya con el dia,
Y á mí esperándome están,
Dame un broquel, y tú aquí
Me espera.
Calab. ¿Yo esperar?
Lisardo. Sí.
CalabEspere
. un judío de Oran;
Que á casa donde encerrado
Estuviste, y áun corrido,
Y hay padre de conocido
Y galan de imaginado,
No has de ir solo.
Lisardo. Sí he de ir.
(Sale Don Félix.)
D. Félix
¿Dónde,
. Lisardo?
Lisardo. No sé
Cómo callaros podré,
Ni cómo os podré decir
Lo que en Ocaña me pasa.
¿Teneis que hacer ahora?
D. Félix. ¿Yo?
Ni en toda esta noche.
Lisardo. ¿No?
D. Félix
No,. que el fuego que me abrasa,
Por acrecentar su ardor,
Treguas por ahora ha dado.
Lisardo
Pues
. yo quiero mi cuidado
Fiaros ya sin temor;
Que si hasta aquí he suspendido
La relacion que empecé,
Respeto que os tuve fué;
Pero habiendo ya sabido
Que nada os puede tocar
Y sois quien sois en efeto,
De mi amor todo el secreto
Hoy os tengo de fiar.
Venid conmigo, y sabreis,
Porque el tiempo no perdamos,
Extraños sucesos.
D. Félix. Vamos;
Que mucha merced me hareis
En divertir el dolor,
De que mi pecho está lleno;
Porque de amor el veneno
Cure triaca de amor.
CalabYo
. ¿qué he de hacer?
Lisardo. Esperar
Aquí en casa á que vengamos.
(Vanse Don Félix y Lisardo.)

ESCENA VI.
CALABAZAS.

¡Buenos, paciencia, quedamos,


Sin ver ni oir, á callar!
Cuando no tiene el servir
Otro gusto, otro placer,
Que escuchar para saber,
Y saber para decir,
Aun deste gusto me priva
El recatarse de mí.
Pues no ha de pasar así;
Así Calabazas viva,
Que por aquel mismo caso
Que aquí de mí se guardó
Tengo de seguirle yo.
Tras ellos, paso entre paso,
Tengo de irme rebozado;
Porque si yo, cual sospecho,
No le murmuro y acecho,
¿Para qué soy su criado?
(Vase.)

Camino de Ocaña.

ESCENA VII.
FABIO, LELIO.

Lelio.Aliéntate, que ya estás


Cerca de Ocaña, señor.
FabioEs
. tan notable el dolor,
Lelio, que no puedo más;
Que aunque yo, por descansar,
De la yegua me apeé,
Y quise venir á pié
Este rato, por dejar
Con ejercicio vencido
El dolor de la caida,
Te confieso que en mi vida
No me he visto tan rendido.
Lelio.Ello fué dicha, señor;
Pues apénas una legua
Andada, cayó la yegua,
Porque pudieras mejor
Volverte á tu casa, donde
Con más cuidado podrás
Curarte.
Fabio. A esta pierna más
Todo el dolor corresponde,
Que fué la que me cogió
Debajo.
Lelio. Súbete, pues
Irás ántes.
Fabio. Mejor es
Andar otro poco, y no
Dejar, Lelio, resfriar
La caida.
Lelio. Dices bien;
Mas considero tambien
Que ya ha empezado á cerrar
La noche, y que lo que andado
En tal parte se mejora,
Se llega más á deshora
A tu casa, y quizás, cuando
Ya recogida, no habrá
Modo de curarte.
Fabio. Bien
Dices: la yegua preven,
Que atada á ese tronco está,
Y vamos, si esto restaura
Mi salud; aunque yo creo
Que ir á casa no deseo,
Por no dar cuidado á Laura,
Que me quiere de manera,
Que temo que hoy ha de ser
Su fin, si me ve volver
Con una pena tan fiera.
Lelio.Como hija, claro está
Que lo sienta mi señora.
FabioPondré
. que aquesta es la hora
Que está recogida ya.
Lelio.¿Quién lo duda?
Fabio. ¡Oh cuánto siento
Haberla de despertar!
Mas no lo puedo excusar.
Lo que haré será, que atento
A su quietud, llamaré
Por la puerta principal;
Pues con prevencion igual
Podrá ser, pues que se ve
De su cuarto más distante,
No oirme.
Lelio. Dispon agora
Tu salud, que mi señora
Lo estimará.
Fabio. No te espante
Verme con tanta fineza;
Que soy en mi senectud,
Amante de su virtud,
Como otros de su belleza.
(Vanse.)

Calle próxima á la casa de Fabio.

ESCENA VIII.
LISARDO, DON FÉLIX; despues CALABAZAS.

D. Félix
Mucho
. me he holgado de oiros,
Por ser la novela extraña.
Lisardo
Esto
. es por mayor; que dejo
De contar mil circunstancias,
Por no cansaros, Don Félix;
Y pues sabeis que me aguarda,
Idos con Dios, que ya es la hora.
D. Félix
Decirme
. á mí que una dama
Vais á ver, y haberme dicho
Que tuvisteis en su casa
Riesgo, y decir que me quede,
Son dos cosas muy contrarias;
Pues no soy de los amigos
Yo, con quien solo se hablan
Las cosas; que precio más
Las obras, que las palabras.
Id á lograr vuestro amor
Norabuena, que hasta el alba
Yo sabré estar en la calle.
Lisardo
A amistad,
. Don Félix, tanta,
Mal hiciera en resistirme.
(Sale Calabazas acechando.)
Calab(Ap.)
. Si cual veo lo que andan,
Lo que hablan viera, yo viera
Lo que andan y lo que hablan.
Llegarme quiero.
Lisardo. ¿Qué es esto?
D. Félix
Un.hombre, si no me engaña
La vista, que tras nosotros
Viene.
Lisardo.Pues sacad la espada.
D. Félix
¿Quién
. va?
Calab. Nadie ya; porque
No diz que va el que se pára.
D. Félix
¿Quién
. sois?
Calab. Un hombre de bien.
Lisardo
Pues
. pase, si acaso pasa.
CalabNo
. paso, que me hago hombre.
D. Félix
Pues. jugaré yo de espadas.
Lisardo
Dadle
. la muerte.
Calab. ¡Detente!
¡Ay, ay! Señor, que me matas;
Que soy Calabazas.
D. Félix. ¿Quién?
CalabCalabazas.
.
Lisardo. Calabazas,
¿Qué es esto?
Calab. Es venir á ver
Dónde vais.
(Danle los dos.)
D. Félix. ¡Por Dios...!
Calab. Ya basta.
Lisardo
Dejadle;
. no alboroteis,
Porque está cerca la casa
Que buscamos.
D. Félix. ¿Hácia aquí
Vive, Lisardo, la dama
Que venís á ver?
Lisardo. Sí, Félix.
D. Félix
¿Y .es bizarra?
Lisardo. Muy bizarra.
D. Félix
¿Tiene
. padre?
Lisardo. Sí.
D. Félix. ¿Y aquí
Os cerrasteis en la cuadra?
Lisardo
Sí..
D. Félix
¿Y .estando ella con vos,
Entró la que me buscaba?
Lisardo
Sí..
D. Félix
Ved. que como la noche
Llena está de sombras pardas,
Más oscura que otras veces,
Pues áun la luna la falta,
Podrá ser que os engañeis.
Lisardo
No. me engaño. A esta ventana
He de llamar, y esta puerta
Han de abrir.
Calab(Ap.)
. Ya sé la casa.
D. Félix . ¿Esta ventana? ¿Esta puerta?
(Ap.)
¡Ay de mí, el cielo me valga,
Que estas las de Laura son,
Para mí dos veces falsas!
Lisardo
Retiraos,
. porque yo
La seña, que es esta, haga.
(Hace la seña á la reja.)
D. Félix
Si mal
. no me acuerdo (¡ay triste!)
En la relacion pasada
Dijisteis que la mujer,
Que para hablaros aguarda,
Es la que hoy escondida
Dentro de mi cuarto estaba.
Lisardo
Es. verdad.
D. Félix. Y que la otra
Que vino...

ESCENA IX.
CELIA. — Dichos.

Celia(En
. la ventana.) Ce.
Lisardo. Ya me llaman.
Celia¿Es
. Lisardo?
Lisardo. Sí, yo soy.
D. Félix . Celia es ésta.
(Ap.)
Celia. Pues aguarda,
Abriré la puerta.
Lisardo. Ya
Conmigo habló la criada,
Y dice que viene á abrirme
La puerta.
D. Félix. Antes que la abra,
Decid...
(Abre la puerta Celia.)
Lisardo. No puede ser ántes.
D. Félix
Si es...
.
Lisardo.Adios, porque me aguarda.
D. Félix
La .dama...
Celia. Entrad presto.
Lisardo. Luégo
Hablarémos. (Éntrase.)
(Al entrar Lisardo, quiere entrar Don Félix, y Celia cierra la puerta.)
ESCENA X.
DON FÉLIX, CALABAZAS.

D. Félix. ¡Y en la cara
Con la puerta me dió Celia!
CalabCon
. cerradura no agravia
Una puerta, aunque es de palo;
Que el tener hierro la salva.
D. Félix. ¿Qué es lo que pasa por mí?
(Ap.)
¿Quién vió confusiones tantas?
¿En casa de Laura, ¡cielos!
Viene buscando la dama
Que hoy de mi cuarto salió
Cuando entró en mi cuarto Laura?
Luego ella no puede ser.
Mas ¿quién ser puede en su casa?
¡Oh quién no la hubiera dicho
A Marcela que dejara
Para mañana el venir
Aquí; que ella lo apurara!
Pero miéntras más discurro,
Más lugar doy á mi infamia.
Pues no discurramos, celos,
Sino á ver la verdad clara
Caminemos más aprisa;
Pues ella es Laura, ó no es Laura:
Si no es ella, ¿qué se pierde
En desengañar mis ánsias?
¿Y qué se pierde, si es ella,
En perder la vida y alma,
Despues de Laura perdida?
La puerta en el suelo caiga.
Pero ¿cómo á esto me atrevo,
Si á Lisardo la palabra
Le he dado? ¿Pero qué importa
La amistad, la confianza,
El respeto, ni el decoro?
Que donde hay celos se acaba
Todo, porque no hay honor
Ni amistad que tanto valga.
(Da golpes á la puerta, para derribarla, y al mismo tiempo; más léjos, dan tambien
golpes dentro.)
Calab¿Qué
. haces, señor?
D. Félix. Darte muerte...
CalabSi
. es posible, no lo hagas.
D. Félix
Mas. ¿qué golpes son aquellos?
Calab¿De
. qué te admiras y espantas?
Otro será en otra parte
Que le habrá dado otra rabia,
Y da golpes á otra puerta.
Fabio(Dentro.)
. Abre aquí, Celia, abre, Laura.
Celia(Dentro.)
. Mi señor es, ¡ay de mí!
D. Félix
Fabio
. es aquel.
(Cuchilladas dentro.)
Fabio(Dentro.)
. ¡Esta infamia
Llego á ver!
Calab. Por Dios, que allá
Ya han llegado á las espadas.
D. Félix
¡Mal
. haya la puerta!
Calab. Amén.
(Vanse.)
Sala en casa de Fabio. — La escena está á oscuras.

ESCENA XI.
LISARDO, con MARCELA en los brazos; despues FÉLIX y
CALABAZAS.

Lisardo
No. temais, señora, nada;
Que, aunque llaman á esta puerta,
Seguro es quien á ella llama.
Marcela
Con. vos, Lisardo, he de ir;
Que como yo á vuestra casa
Llegue, nada hay que temer,
Si es que ella una vez me ampara.
Lisardo
Venid,
. y no os receleis
De un hombre que me acompaña.
Marcela
¿Es. Félix?
Lisardo. Sí.
Marcela. Pues mirad
Que es Félix...
Lisardo. ¿En qué reparas?
Ya no es tiempo de recatos.—
(Salen Don Félix y Calabazas.)
¿Félix?
D. Félix.¿Quién va?
Lisardo. Mis desgracias.
D. Félix
¿Qué
. ha sido aquesto?
Lisardo. Que estando
Hablando con esta dama,
Vino su padre de fuera,
Llamó, y viendo que tardaban
En abrirle, derribó
La puerta y sacó la espada.
Porque se apagó la luz
Tuve lugar de librarla.
Llevadla; que yo me quedo
A guardaros las espaldas,
Para que ninguno os siga;
Que conmigo Calabazas
Quedará.
Calab. No quedará.
D. Félix
Mejor
. es con ella vaya,
Y nos quedemos los dos.
Lisardo
¿Tan
. sola hemos de dejarla?
No es razon; pues la primera
Obligacion es la dama
En todo trance; así, Félix,
Vos solo habeis de llevarla,
Y ponerla en salvo.
D. Félix. Es justo.
¿En fin, has venido, Laura, (A Marcela.)
A mi poder?
Marcela .
(Ap.) ¡Ay de mí!
D. Félix . Yo estoy muerto.
(Ap.)
Marcela .
(Ap.) Estoy turbada.
D. Félix
Ven. conmigo; que aunque no
Mereces finezas tantas,
Soy quien soy, y he de librarte.
Marcela
¡Hay. mujer más desdichada!
D. Félix
¡Hay. hombre más infelice!
(Vanse Don Félix y Marcela.)
ESCENA XII.
FABIO, LELIO, con luz, y criados con las espadas desnudas. —
LISARDO, CALABAZAS.

FabioAunque
. las fuerzas me faltan,
No las fuerzas del honor
Para tomar mil venganzas.
Lisardo
Deteneos,
. que ninguno
De aquí ha de pasar.
Fabio. Mi espada
Hará paso por el pecho
Vuestro.
(Riñen todos.)
Calab. ¡Infeliz Calabazas!
¿Quién te metió en acechar?
Lisardo . Pues que ya Félix se alarga,
(Ap.)
Antes que aquí me conozcan
Mejor es volver la espalda;
Esto es valor, no temor.
(Vase.)
FabioEspera,
. cobarde, aguarda.
Calab(Ap.)
. ¿Quién creyera que Lisardo
En la ocasion me dejara?
Lelio.Aquí se quedó uno dellos.
FabioPues
. muera, Lelio. ¿Qué aguardas?
CalabDeteneos,
. ¡por Dios!
Fabio. ¿Quién sois?
CalabSi
. es que el miedo no me engaña,
Un curioso impertinente.
FabioDejad
. la espada.
Calab. La espada
Es poca cosa; el sombrero,
La daga, el broquel, la capa,
La ropilla y los calzones.
Fabio¿Sois
. criado del que agravia
Esta casa?
Calab. Sí señor;
Porque es un agravia-casas,
Que no se puede sufrir.
Fabio¿Quién
. es, y cómo se llama?
CalabLisardo
. se llama, y es
Un soldado, camarada
De Félix.
Fabio. Porque no empiece
Por la menor mi venganza,
No te doy muerte.
Calab. Haces bien.
FabioY. pues alguna luz hallan
Mis desdichas, á buscar
Iré á Félix. ¡Oh, mal haya
Casa con dos puertas, pues
Tan mal el honor se guarda!
(Vanse.)

Casa de Don Félix.

ESCENA XIII.
DON FÉLIX y MARCELA, á oscuras; despues HERRERA, LAURA y
SILVIA.
D. Félix .
(Dentro.) ¡Hola! traed aquí una luz.
Herrer .
(Dentro.) Ya la llevo, si es que hallan
Luz unos ojos dormidos.
(Salen al paño Laura y Silvia.)
Laura(A
. Silvia.) Ya dentro del cuarto andan:
Escuchemos desde aquí.
D. Félix
Ya .por lo ménos, ingrata,
Ya por lo ménos no puedes
Negarme...
Laura(Ap.)
. Con mujer habla.
D. Félix
En .este lance, que eres
Mudable, inconstante, falsa,
Cruel, aleve, engañosa;
Pues á nadie desengañan
Más cara á cara sus celos.
Marcela . Aquí mi vida se acaba.
(Ap.)
D. Félix
¿Para
. esto viniste hoy
A mi casa?
Laura(Ap.)
. La que estaba
Tapada hoy es, pues la dice
Que hoy ha venido á su casa.
D. Félix
En .mi poder estás, mira
Si habrá disculpa. ¡Mal haya
Cuanto tiempo te he querido,
Cuantas penas, cuantas ánsias
Padecí, y cuantas finezas
Hizo mi amor por tu causa!
Laura¿No
. escuchas cómo confiesa
Que la ha querido? ¿Qué aguarda
Mi paciencia?
Silvia. ¿Dónde vas?
LauraNo
. sé. (¡Ay Silvia, estoy turbada!)
A escucharle de más cerca.
D. Félix
¡Oh. cuánto con la luz tardas!
Herrer .
(Dentro.) Ya va la luz.
Marcela
(Ap.). ¿Qué he de hacer,
Si la trae?
D. Félix. ¿No dices nada?
Pero si estás convencida,
¿Qué has de decir?
(Suéltala de la mano, vase retirando Marcela; y Laura viene á ponerse en medio
de las dos; él la coge la mano, entendiendo que es Marcela.)
Marcela .
(Ap.) ¡Oh si hallara
Por donde irme; que á lo ménos
La vida así asegurara!
D. Félix
Detente,
. no huyas, no huyas;
Que no quiero más venganza
De tí, que sepas que sé
Esto.
Laura(Ap.)
. Por otra me habla,
Y he de callar mis agravios
Hasta que las luces traigan,
Y vea que yo soy con quien
Está.
Marcela . Confusa y turbada,
(Ap.)
La puerta hallé de mi cuarto;
Este sagrado me valga,
Pues fué dicha estar abierta.
Silvia¿Eres
. Laura?
Marcela. No soy Laura.
¿Eres tú Silvia?
Silvia. Yo soy.
¿Qué es esto?
Marcela. Fortunas várias.
Cierra esa puerta, y conmigo
Ven, Silvia, aprisa. ¿Qué aguardas?
(Vanse, cerrando tras sí la puerta.)

ESCENA XIV.
DON FÉLIX, LAURA; HERRERA, que saca luz.

Herrer
Ya. están las luces aquí.
D. Félix
Déjalas,
. y afuera aguarda.
(Vase Herrera, y cierra la puerta Don Félix.)
Laura(Ap.)
. ¡Aquí es ello, cuando vuelva
A verme!
D. Félix. En efecto, Laura,
Yo soy quien solo guardó
A sus celos las espaldas.
Laura(Ap.)
. ¿Qué es esto? ¿Cómo de verme
Ni se turba ni embaraza?
D. Félix
Sólo
. yo en el mundo traje
Para otro galan su dama.
Dí agora que yo te ofendo.
Laura¡No
. está la deshecha mala!
¡Bien te alientas á fingir
La razon con que me agravias;
Pues viéndote convencido,
Cuando en tus brazos me hallas,
De haberme hablado por otra
A quien traes á tu casa,
Prosigues las quejas della
Conmigo!
D. Félix. Sólo eso falta
A mi paciencia ofendida,
Que tú agora creer me hagas
Que hablaba con otra yo.
Laura¿Pues
. de qué, Félix, te espantas,
Si es verdad?
D. Félix. ¿Pues dónde está
La mujer con quien yo hablaba?
LauraSi
. una casa con dos puertas
Mala es de guardar, repara
Que peor de guardar será,
Con dos puertas una sala.
Ya se fué.
D. Félix. Laura, por Dios,
Que me dejes. Véte, Laura,
Que me harás perder el juicio,
Si quieres que yo no haya
Traídote aquí, porque
Estando (la voz me falta)
Tu padre fuera, Lisardo...
No puedo hablar.
Laura. Tú te engañas;
Que yo escondida esta noche
En el cuarto de tu hermana
He estado, por sólo ver
Esto que á los dos nos pasa;
Y ella...
D. Félix.Detente, que ahora
Lo veré.—¡Marcela, hermana!

ESCENA XV.
MARCELA, SILVIA. — DON FÉLIX, LAURA.
Marcela
¿Qué. quieres? (Ap. Disimular
Importa, pues informada
Estoy de todo.)
D. Félix. Dí, ¿ha estado
Contigo esta noche Laura?
Marcela
¿Laura
. conmigo, señor.
A qué efecto? Yo mañana
Habia de ir á estar con ella;
Pero ¡ella conmigo!
Laura. Aguarda.
¿No vine esta tarde yo
A pedirte que en tu casa
Me tuvieras? ¿Y á la mia
Tú...?
MarcelaNo. prosigas, que nada
De eso es verdad.
D. Félix. Laura, ¿ves
Qué mal te salió la traza?
¿Estáse esotra en su cuarto
Recogida y retirada,
Y dices que estás con ella?
LauraPues
. tú, Marcela, me agravias.
Marcela
(Ap.. á Laura.) Sí, que soy primero yo.
LauraPues
. tanto me apuras, salgan
Verdades á luz. Marcela
Ha sido...
(Llaman dentro.)
Silvia. A la puerta llaman.
Lisardo .
(Dentro.) Abrid, Don Félix.
D. Félix. Agora
Verás que todo se acaba;
Pues tu galan, Laura, viene.
LauraAhí
. tengo yo mi esperanza.
Marcela . Aquí se deshace todo.
(Ap.)
¡Quién á Lisardo avisara
De mi peligro!
(Retírase á un lado.)

ESCENA XVI.
LISARDO. — Dichos.

Lisardo. Don Félix,


Porque ninguno llegara
A seguirme, tardé. ¿Dónde
Habeis puesto aquella dama?
D. Félix
Veisla
. aquí; pero primero
Que acabe con mi esperanza
El verla en vuestro poder,
Me habeis de sacar el alma.
Lisardo
Hasta
. agora no creí
Que caballeros engañan
De vuestras obligaciones
A los que dellos se amparan.
La dama que os entregué,
Os pido.
D. Félix.¿No es esta dama
La que me entregasteis?
Lisardo. No.
D. Félix
¡Sólo
. aquesto me faltaba
Para acabar de perder
La paciencia!
Marcela .
(Ap.) ¡Ay desdichada!
Lisardo
Si .esta suponeis, Don Félix,
Porque os obliga otra causa,
Hablad más claro conmigo.
LauraYo
. de confusiones tantas
Os sacaré.—Dí, Lisardo,
¿Es ésta á quien buscas y amas?
Lisardo
Esta
. es. Sí, aquí la teneis.
¿Qué os ha obligado á ocultarla?
Laura(A
. Don Félix.) ¡Mira si estaba en su cuarto,
Recogida y retirada!
Primero soy yo, Marcela. (Ap. á ella.)
D. Félix
Corrido
. estoy; esta daga
Dé á una vil hermana muerte.
Marcela
Lisardo,
. mi vida ampara.
Lisardo .
(Poniéndose delante.)
¿Hermana de Félix sois?
D. Félix
Y en. quien tomaré venganza.
Lisardo
Sabeis
. quién soy, y es preciso
Defenderla y ampararla
Por mujer.
D. Félix. Tambien sabeis
Quién yo soy, y que en mi casa
Ménos que quien sea su esposo,
No ha de atreverse á mirarla.
Lisardo
Luego
. con serlo quedamos
Bien los dos.

ESCENA XVII.
FABIO, CALABAZAS, criados. — Dichos.
Fabio. Esta es la casa,
Entrad.
D. Félix.¿Qué es esto?
Fabio. Esto, Félix,
Es honor.
Calab(Ap.)
. ¡Qué linda danza
Se va urdiendo!
Fabio. ¿Dónde está
Un Lisardo, camarada
Vuestro?
Lisardo. Yo soy; porque nunca
A nadie escondí la cara.
Calab(Ap.)
. Nunca la cara escondió,
Pero volvió las espaldas.
Fabio¡Oh
. traidor!
D. Félix. Fabio, teneos;
(Pónense los dos á un lado.)
Que la cólera os engaña.
El enojo que traeis,
Si ha sido la ocasion Laura,
Es conmigo, y me ha tocado
Como á mi esposa guardarla.
FabioNo
. tengo qué responderos.
Si Laura con vos se casa.
D. Félix
Pues
. para que veais si es cierto,
Aquesta es mi mano, Laura.
Y pues el haber tenido
Dos puertas esta y tu casa,
Causa fué de los engaños
Que á mí y Lisardo nos pasan,
De la Casa con dos puertas,
Aquí la comedia acaba.
LA DAMA DUENDE.

You might also like