2424from .utils .cli import (
2525 get_rich_toolkit ,
2626 get_uvicorn_log_config ,
27+ should_use_rich_toolkit ,
2728 should_use_rich_uvicorn_logs ,
2829)
2930
@@ -144,15 +145,20 @@ def _run(
144145 forwarded_allow_ips : str | None = None ,
145146 public_url : str | None = None ,
146147) -> None :
147- with get_rich_toolkit () as toolkit :
148+ use_rich = should_use_rich_toolkit ()
149+ with get_rich_toolkit (use_rich = use_rich ) as toolkit :
148150 server_type = "development" if command == "dev" else "production"
149151
150- toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
151- toolkit .print_line ()
152+ if use_rich :
153+ toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
154+ else :
155+ toolkit .print_title ("⚡️ Starting FastAPI" )
152156
153- toolkit .print (
154- "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
155- )
157+ if use_rich :
158+ toolkit .print_line ()
159+ toolkit .print (
160+ "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
161+ )
156162
157163 if entrypoint and (path or app ):
158164 toolkit .print_line ()
@@ -198,69 +204,77 @@ def _run(
198204 module_data = import_data .module_data
199205 import_string = import_data .import_string
200206
201- toolkit .print (f"Importing from { module_data .extra_sys_path } " )
202- toolkit .print_line ()
203-
204- if module_data .module_paths :
205- root_tree = _get_module_tree (module_data .module_paths )
206-
207- toolkit .print (root_tree , tag = "module" )
207+ if use_rich :
208+ toolkit .print (f"Importing from { module_data .extra_sys_path } " )
208209 toolkit .print_line ()
209210
210- toolkit .print (
211- "Importing the FastAPI app object from the module with the following code:" ,
212- tag = "code" ,
213- )
214- toolkit .print_line ()
215- toolkit .print (
216- f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
217- )
218- toolkit .print_line ()
219-
220- toolkit .print (
221- f"Using import string: [blue]{ import_string } [/]" ,
222- tag = "app" ,
223- )
211+ if module_data .module_paths :
212+ root_tree = _get_module_tree (module_data .module_paths )
224213
225- mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
226- app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
227- toolkit .print_line ()
228- toolkit .print ("Configuration sources:" , tag = "info" )
229- if mod_source_desc == app_source_desc :
230- toolkit .print (f" • Import string: { mod_source_desc } " )
231- else :
232- toolkit .print (f" • Module: { mod_source_desc } " )
233- toolkit .print (f" • App name: { app_source_desc } " )
214+ toolkit .print (root_tree , tag = "module" )
215+ toolkit .print_line ()
234216
235- if import_data .module_config_source == "auto-discovery" :
217+ toolkit .print (
218+ "Importing the FastAPI app object from the module with the following code:" ,
219+ tag = "code" ,
220+ )
236221 toolkit .print_line ()
237222 toolkit .print (
238- "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
239- tag = "tip" ,
223+ f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
240224 )
241225 toolkit .print_line ()
226+
242227 toolkit .print (
243- Syntax (
244- (
245- "[tool.fastapi]\n "
246- f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
247- ),
248- "toml" ,
249- theme = "ansi_light" ,
250- )
228+ f"Using import string: [blue]{ import_string } [/]" ,
229+ tag = "app" ,
251230 )
231+ else :
232+ toolkit .print (f"🐍 App: [blue]{ import_string } [/]" )
233+
234+ if use_rich :
235+ mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
236+ app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
237+ toolkit .print_line ()
238+ toolkit .print ("Configuration sources:" , tag = "info" )
239+ if mod_source_desc == app_source_desc :
240+ toolkit .print (f" • Import string: { mod_source_desc } " )
241+ else :
242+ toolkit .print (f" • Module: { mod_source_desc } " )
243+ toolkit .print (f" • App name: { app_source_desc } " )
244+
245+ if import_data .module_config_source == "auto-discovery" :
246+ toolkit .print_line ()
247+ toolkit .print (
248+ "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
249+ tag = "tip" ,
250+ )
251+ toolkit .print_line ()
252+ toolkit .print (
253+ Syntax (
254+ (
255+ "[tool.fastapi]\n "
256+ f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
257+ ),
258+ "toml" ,
259+ theme = "ansi_light" ,
260+ )
261+ )
252262
253263 url = public_url .rstrip ("/" ) if public_url else f"http://{ host } :{ port } "
254264 url_docs = f"{ url } /docs"
255265
256- toolkit .print_line ()
257- toolkit .print (
258- f"Server started at [link={ url } ]{ url } [/]" ,
259- f"Documentation at [link={ url_docs } ]{ url_docs } [/]" ,
260- tag = "server" ,
261- )
266+ if use_rich :
267+ toolkit .print_line ()
268+ toolkit .print (f"Server started at [link={ url } ]{ url } [/]" , tag = "server" )
269+ toolkit .print (
270+ f"Documentation at [link={ url_docs } ]{ url_docs } [/]" , tag = "server"
271+ )
272+ else :
273+ toolkit .print (f"🌐 Server: [link={ url } ]{ url } [/]" )
274+ toolkit .print (f"📚 Docs: [link={ url_docs } ]{ url_docs } [/]" )
275+ toolkit .print ("" )
262276
263- if command == "dev" :
277+ if command == "dev" and use_rich :
264278 toolkit .print_line ()
265279 toolkit .print (
266280 "Running in development mode, for production use: [bold]fastapi run[/]" ,
@@ -272,9 +286,10 @@ def _run(
272286 "Could not import Uvicorn, try running 'pip install uvicorn'"
273287 ) from None
274288
275- toolkit .print_line ()
276- toolkit .print ("Logs:" )
277- toolkit .print_line ()
289+ if use_rich :
290+ toolkit .print_line ()
291+ toolkit .print ("Logs:" )
292+ toolkit .print_line ()
278293
279294 extra_uvicorn_kwargs : dict [str , Any ] = {}
280295 if should_use_rich_uvicorn_logs ():
0 commit comments