<% 
import sys, copy
import py_cmdtabs.cli_manager as cli_manager 

def show_n_exec(table_name, args, meth, skip_input=False, out='std', supp_tables=[]):
	results = plotter.renderize_child_template(plotter.get_internal_template('show_n_exec'), 
				table_name=table_name, out_type= out, args=args, library=cli_manager, 
				method=meth, skip_input=skip_input, supp_tables = supp_tables
			)
	return results

def create_tool_section(tool_name, title_id, container_id):
	title = plotter.create_title(tool_name, id=title_id, hlevel=2, css_class='dummy', indexable=True, clickable=True, t_id=container_id, clickable_text = '(Click me)')	
	content = plotter.renderize_child_template(plotter.get_internal_template(tool_name), show_n_exec = show_n_exec )
	container = plotter.create_collapsable_container(container_id, content, display="visible")
	return f'{title}\n{container}\n'

%>

<div style="width: 95%; padding:20px;">
	<% plotter.set_header() %>
	<h1> Operation with tables </h1>

		<h2> General guidelines </h2>
			<p> Although it will be further explain in each corresponding binary section, there are a series of guidelines that work for the majority of commands. </p>
			<p> For the binaries that are given an input table flag (in most cases with the -i flag, but in other cases such as intersect_columns with the -a and -b flags), these tables can be provided in plain text format, but they can also be loaded compressed in gzip format using the --compressed_in flag. For example: </p>

			<div class='code'>
				cmdtabs --desaggregate -i patients_and_phens.gz --desagg_col 2 --compressed_in -o desaggregated.txt
			</div>

			<p> Additionally, for commands that require only a single input table (such as aggregate_column_data, desaggregate_column_data, or transpose_table, but not intersect_columns), a dash - can be used with the input flag to indicate that the table content comes from stdout (for example, as the result of piping from a previous command), and it may also be compressed. For example: </p>

			<div class='code'>
				cat patients_and_phens.txt | script_que_devuelve_datos_comprimidos | cmdtabs --desaggregate -i - --desagg_col 2 --compressed_in
			</div>

			<p> In scripts that produce some type of output (generally using the -o flag), similarly, the --compressed_out flag can be used so that the result is returned compressed in gzip format. Additionally, if no output flag is specified (omitting -o output_file) and the script returns only a single table, the content is sent to stdout instead of being written to a file, so it can be piped into another script. For example: </p>

			<div class='code'>
				cmdtabs --desaggregate -i patients_and_phens.txt --desagg_col 2 --compressed_out | script_que_utiliza_datos_comprimidos

			</div>

			<p> Both flags may be used at the same time, recieving and returning a compressed table. For example:  </p>
			<div class='code'>
				cmdtabs --desaggregate -i patients_and_phens.gz --desagg_col 2 --compressed_in --compressed_out -o desaggregated.gz
			</div>

			<p> We can also use pipes: </p>
			<div class='code'>
				cat patients_and_phens.txt | script_que_devuelve_datos_comprimidos | cmdtabs --desaggregate -i - --desagg_col 2 --compressed_in --compressed_out | script_que_utiliza_datos_comprimidos

			</div>

			<p> Finally, the --transposed flag can be used in all scripts that load and/or write tables. When used, the table will be transposed at load time as well as immediately before being written.  </p>
		<% 
			# tool name, html title id, html container id
			tools = [
				['aggregate_column_data', 'agg_from', 'agg_to'],
				['column_filter', 'colfil_from', 'colfil_to'],
				['create_metric_table', 'metr_from', 'metr_to'],
				['desaggregate_column_data', 'desagg_from', 'desagg_to'],
				['excel_to_tabular', 'excel_from', 'excel_to'],
				['filter_by_list', 'filter_from', 'filter_to'],
				['get_columns', 'getcol_from', 'getcol_to'],
				['intersect_columns', 'inter_from', 'inter_to'],
				['merge_tabular', 'merge_from', 'merge_to'],
				['records_count', 'recs_from', 'recs_to'],
				['sort_rows_by', 'sortrows_from', 'sortrows_to'],
				['standard_name_replacer', 'repl_from', 'repl_to'],
				['subset_table', 'subs_from', 'subs_to'],
				['table_linker', 'link_from', 'link_to'],
				['tag_table', 'tag_from', 'tag_to'],
				['transform_to_latex', 'trnsf_from', 'trnsf_to'],
				['transpose_table', 'trnsp_from', 'trnsp_to']
			]
		%>
		% for tool_name, title_id, container_id in tools:
			${create_tool_section(tool_name, title_id, container_id)}
		% endfor	
</div>
