1- """ domible/src/domible/tools.py
1+ """domible/src/domible/tools.py
22
33tools.py has functions for users of the domible package to do simple things
44like open their HTML doc in the browser,
55or save the HTML to a specified file.
66tools.py imports other modules within domible thus should not be used by any of domible's submodules.
7- Any helper functions needed by domible should be put in utils.py
8- See the top level comments in utils.py to understand why tools.py exists.
7+ Any helper functions needed by domible should be put in utils.py
8+ See the top level comments in utils.py to understand why tools.py exists.
99"""
1010
11- from pathlib import Path
11+ from pathlib import Path
1212from tempfile import NamedTemporaryFile
1313import webbrowser as wb
1414
15- from domible .elements import BaseElement
16- from domible .elements import Html , Body , Heading
17- from domible .builders import element_from_object , default_toggle_details_button
18- from domible .starterDocuments import basic_head_empty_body
15+ from domible .elements import BaseElement
16+ from domible .elements import Html , Body , Heading , Div
17+ from domible .builders import element_from_object , default_toggle_details_button
18+ from domible .starterDocuments import basic_head_empty_body
1919
2020
2121def save_to_file (element : BaseElement , filename : str , force : bool = False ) -> None :
@@ -37,39 +37,64 @@ def save_to_file(element: BaseElement, filename: str, force: bool = False) -> No
3737 f .write (f"{ element } " )
3838
3939
40- def open_html_in_browser (html_doc : Html , save_file : str = None , force : bool = False ) -> None :
40+ def open_html_document_in_browser (
41+ html_doc : Html , save_file : str = None , force : bool = False
42+ ) -> None :
4143 """
4244 open the html_doc in the default browser.
4345 if a save_to_file is provided, also save the html_doc to that file,
44- else use a temporary file
46+ else use a temporary file
4547 """
4648 if save_file :
4749 save_to_file (html_doc , save_file , force )
48- # path must be absolute to match how temp file works
49- path = str (Path (save_file ).absolute ())
50- else :
51- path = NamedTemporaryFile (delete = False , suffix = ' .html' )
52- f = open (path .name , ' w+t' , encoding = ' utf-8' )
50+ # path must be absolute to match how temp file works
51+ path = str (Path (save_file ).absolute ())
52+ else :
53+ path = NamedTemporaryFile (delete = False , suffix = " .html" )
54+ f = open (path .name , " w+t" , encoding = " utf-8" )
5355 f .write (f"{ html_doc } " )
5456 f .close ()
55- path = path .name # consistent with path from saving file
56- wb .open (' file://' + path )
57+ path = path .name # consistent with path from saving file
58+ wb .open (" file://" + path )
5759
5860
59- def open_object_in_browser (obj : object , depth :int = 42 , title : str = "opening an object in the browser" , save_file : str = None , force : bool = False ) -> None :
61+ def open_html_fragment_in_browser (
62+ html_frag : BaseElement ,
63+ title : str = "opening HTML fragment" ,
64+ save_file : str = None ,
65+ force : bool = False ,
66+ ) -> None :
6067 """
61- get HTML representation of the object then open it in the default browser.
68+ open some bit of HTML you've created in the default browser,
69+ using the default basic HTML doc.
70+ This is useful if you have some HTML you want to view in a browser
71+ and don't want to create the document and get the body and so on...
72+ This is sort of analogous to open_object_in_browser
73+ """
74+ html_doc : Html = basic_head_empty_body (title )
75+ body : Body = html_doc .get_body_element ()
76+ body .add_content (html_frag )
77+
6278
63- the passed in object is used to get HTML using element_from_object.
64- That HTML is then put into a simple HtML document using the basicHeadEmptyBody starter document.
65- that simple document is then opened in the browser.
79+ def open_object_in_browser (
80+ obj : object ,
81+ depth : int = 42 ,
82+ title : str = "opening an object in the browser" ,
83+ save_file : str = None ,
84+ force : bool = False ,
85+ ) -> None :
86+ """
87+ get HTML representation of the object then open it in the default browser.
6688 """
6789 obj_html = element_from_object (obj , depth )
68- html_doc : Html = basic_head_empty_body (title )
69- body : Body = html_doc .get_body_element ()
70- body .add_content ([Heading (1 , f"showing object of type { type (obj ).__name__ } " ), default_toggle_details_button ()])
71- body .add_content (obj_html )
72- open_html_in_browser (html_doc , save_file , force )
90+ frag : Div = Div (
91+ [
92+ Heading (1 , f"showing object of type { type (obj ).__name__ } " ),
93+ default_toggle_details_button (),
94+ obj_html ,
95+ ]
96+ )
97+ open_html_fragment_in_browser (frag , title , save_file , force )
7398
7499
75100## end of file
0 commit comments