2020
2121from . import __version__
2222from .logging import setup_logging
23- from .utils .cli import get_rich_toolkit , get_uvicorn_log_config
23+ from .utils .cli import get_rich_toolkit , get_uvicorn_log_config , should_use_rich_toolkit
2424
2525app = typer .Typer (
2626 rich_markup_mode = "rich" , context_settings = {"help_option_names" : ["-h" , "--help" ]}
@@ -139,15 +139,20 @@ def _run(
139139 forwarded_allow_ips : str | None = None ,
140140 public_url : str | None = None ,
141141) -> None :
142- with get_rich_toolkit () as toolkit :
142+ use_rich = should_use_rich_toolkit ()
143+ with get_rich_toolkit (use_rich = use_rich ) as toolkit :
143144 server_type = "development" if command == "dev" else "production"
144145
145- toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
146- toolkit .print_line ()
146+ if use_rich :
147+ toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
148+ else :
149+ toolkit .print_title ("⚡️ Starting FastAPI" )
147150
148- toolkit .print (
149- "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
150- )
151+ if use_rich :
152+ toolkit .print_line ()
153+ toolkit .print (
154+ "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
155+ )
151156
152157 if entrypoint and (path or app ):
153158 toolkit .print_line ()
@@ -193,69 +198,76 @@ def _run(
193198 module_data = import_data .module_data
194199 import_string = import_data .import_string
195200
196- toolkit .print (f"Importing from { module_data .extra_sys_path } " )
197- toolkit .print_line ()
198-
199- if module_data .module_paths :
200- root_tree = _get_module_tree (module_data .module_paths )
201-
202- toolkit .print (root_tree , tag = "module" )
201+ if use_rich :
202+ toolkit .print (f"Importing from { module_data .extra_sys_path } " )
203203 toolkit .print_line ()
204204
205- toolkit .print (
206- "Importing the FastAPI app object from the module with the following code:" ,
207- tag = "code" ,
208- )
209- toolkit .print_line ()
210- toolkit .print (
211- f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
212- )
213- toolkit .print_line ()
214-
215- toolkit .print (
216- f"Using import string: [blue]{ import_string } [/]" ,
217- tag = "app" ,
218- )
205+ if module_data .module_paths :
206+ root_tree = _get_module_tree (module_data .module_paths )
219207
220- mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
221- app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
222- toolkit .print_line ()
223- toolkit .print ("Configuration sources:" , tag = "info" )
224- if mod_source_desc == app_source_desc :
225- toolkit .print (f" • Import string: { mod_source_desc } " )
226- else :
227- toolkit .print (f" • Module: { mod_source_desc } " )
228- toolkit .print (f" • App name: { app_source_desc } " )
208+ toolkit .print (root_tree , tag = "module" )
209+ toolkit .print_line ()
229210
230- if import_data .module_config_source == "auto-discovery" :
211+ toolkit .print (
212+ "Importing the FastAPI app object from the module with the following code:" ,
213+ tag = "code" ,
214+ )
231215 toolkit .print_line ()
232216 toolkit .print (
233- "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
234- tag = "tip" ,
217+ f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
235218 )
236219 toolkit .print_line ()
220+
237221 toolkit .print (
238- Syntax (
239- (
240- "[tool.fastapi]\n "
241- f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
242- ),
243- "toml" ,
244- theme = "ansi_light" ,
245- )
222+ f"Using import string: [blue]{ import_string } [/]" ,
223+ tag = "app" ,
246224 )
225+ else :
226+ toolkit .print (f"🐍 App: [blue]{ import_string } [/]" )
227+
228+ if use_rich :
229+ mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
230+ app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
231+ toolkit .print_line ()
232+ toolkit .print ("Configuration sources:" , tag = "info" )
233+ if mod_source_desc == app_source_desc :
234+ toolkit .print (f" • Import string: { mod_source_desc } " )
235+ else :
236+ toolkit .print (f" • Module: { mod_source_desc } " )
237+ toolkit .print (f" • App name: { app_source_desc } " )
238+
239+ if import_data .module_config_source == "auto-discovery" :
240+ toolkit .print_line ()
241+ toolkit .print (
242+ "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
243+ tag = "tip" ,
244+ )
245+ toolkit .print_line ()
246+ toolkit .print (
247+ Syntax (
248+ (
249+ "[tool.fastapi]\n "
250+ f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
251+ ),
252+ "toml" ,
253+ theme = "ansi_light" ,
254+ )
255+ )
247256
248257 url = public_url .rstrip ("/" ) if public_url else f"http://{ host } :{ port } "
249258 url_docs = f"{ url } /docs"
250259
251- toolkit .print_line ()
252- toolkit .print (
253- f"Server started at [link={ url } ]{ url } [/]" ,
254- f"Documentation at [link={ url_docs } ]{ url_docs } [/]" ,
255- tag = "server" ,
256- )
260+ if use_rich :
261+ toolkit .print_line ()
262+ toolkit .print (f"Server started at [link={ url } ]{ url } [/]" , tag = "server" )
263+ toolkit .print (
264+ f"Documentation at [link={ url_docs } ]{ url_docs } [/]" , tag = "server"
265+ )
266+ else :
267+ toolkit .print (f"🌐 Server: [link={ url } ]{ url } [/]" )
268+ toolkit .print (f"📚 Docs: [link={ url_docs } ]{ url_docs } [/]" )
257269
258- if command == "dev" :
270+ if command == "dev" and use_rich :
259271 toolkit .print_line ()
260272 toolkit .print (
261273 "Running in development mode, for production use: [bold]fastapi run[/]" ,
@@ -267,9 +279,10 @@ def _run(
267279 "Could not import Uvicorn, try running 'pip install uvicorn'"
268280 ) from None
269281
270- toolkit .print_line ()
271- toolkit .print ("Logs:" )
272- toolkit .print_line ()
282+ if use_rich :
283+ toolkit .print_line ()
284+ toolkit .print ("Logs:" )
285+ toolkit .print_line ()
273286
274287 uvicorn .run (
275288 app = import_string ,
0 commit comments