def find_master_tab(self,tab,as_list=False):
<EOL>if isinstance(tab, int):<EOL><INDENT>tab = self.tab_widget.widget(tab)<EOL><DEDENT>km=tab.kernel_manager<EOL>widget_list = [self.tab_widget.widget(i) for i in range(self.tab_widget.count())]<EOL>filtered_widget_list = [ widget for widget in widget_list if<EOL>widget.kernel_manager.connection_file == km.connection_file and<EOL>hasattr(widget,'<STR_LIT>') ]<EOL>master_widget= [ widget for widget in filtered_widget_list if widget._may_close]<EOL>if as_list:<EOL><INDENT>return master_widget<EOL><DEDENT>assert(len(master_widget)<=<NUM_LIT:1> )<EOL>if len(master_widget)==<NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>return master_widget[<NUM_LIT:0>]<EOL>
Try to return the frontend that owns the kernel attached to the given widget/tab.

    Only finds frontend owned by the current application. Selection
    based on port of the kernel might be inaccurate if several kernel
    on different ip use same port number.

    This function does the conversion tabNumber/widget if needed.
    Might return None if no master widget (non local kernel)
    Will crash IPython if more than 1 masterWidget

    When asList set to True, always return a list of widget(s) owning
    the kernel. The list might be empty or containing several Widget.