def delete_columns(doc, table_name, column_names, box_message=True): """Removes columns from data table args: doc (Spotfire document instance): document to read from table_name (str): name of table col_list (list): names of the columns to be deleted """ table = get_table(doc, table_name) extra_text = 'in table {}'.format(table_name) if column_names: if type(column_names) == str: column_list = [] column_list.append(column_names) column_names = column_list confirmation = deletion_message(column_names, 'column',extra_text) if confirmation: for column_name in column_names: try: table.Columns.Remove(column_name) except ValueError: message = 'No column named {} in table {}'.format(column_name, table_name) if box_message: ok_message(message) else: LOGGER.warning(message) else: no_deletion(column_names, extra_text)
def get_coloraxis(viz_cont): """Gets color axis from visual_content args: viz_content: Spotfire.Dxp.Application.Visuals.VisualContent returns col_axis: Spotfire.Dxp.Application.Visuals.ColorAxis """ col_axis = None if hassatr(viz_cont, 'ColorAxis'): col_axis = viz_cont.ColorAxis else: message = 'Visual has no color axis' ok_message(message) return col_axis
def table_text_writer(list_input): """Writes table input args: list_input (list of lists): input to be written returns: stream """ from System.IO import StreamWriter, MemoryStream, SeekOrigin list_input = controlled_list(list_input) LOGGER.debug(list_input) LOGGER.debug('--------') header = controlled_list(list_input.pop(0)) length_of_header = len(header) head_line = create_line(header) # if only a header line if not list_input: text = ' ' * (length_of_header) else: text ='' LOGGER.debug('Loop') for i, text_list in enumerate(list_input): text_list = controlled_list(text_list) LOGGER.debug(text_list) if len(text_list) != length_of_header: message = ('Line {} in text: [{}] does not have ' + ' same length as header [{}], will be ' + 'skipped').format(i, join_list(text_list), join_list(header)) ok_message(message) else: text += create_line(text_list) text = head_line + text stream = MemoryStream() writer = StreamWriter(stream) writer.Write(text) writer.Flush() stream.Seek(0, SeekOrigin.Begin) return stream
def make_table(doc, table_name, column_names, data=()): """Makes data table in spotfire project args: doc (Spotfire document instance): document to read from table_name (str): name of table column_names (list like): names of the columns in the table """ import clr clr.AddReference('System.Data') import System from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings from Spotfire.Dxp.Data import DataType tables = doc_tables(doc) col_lengths = len(column_names) -1 if col_lengths <1: message = ('You are trying to create an empty table without ' + 'any data columns, this is useless, process stopped!') ok_message(message) else: text_input = [column_names] text_input.extend(data) LOGGER.debug(text_input) stream = table_text_writer(text_input) readerSettings = TextDataReaderSettings() readerSettings.Separator = "\t" readerSettings.AddColumnNameRow(0) for i, col_n in enumerate(controlled_list(column_names)): readerSettings.SetDataType(i, DataType.String) LOGGER.debug(col_n + " Added as column " + str(i)) source = TextFileDataSource(stream, readerSettings) if tables.Contains(table_name): tables[table_name].ReplaceData(source) else: tables.Add(table_name, source)
def _delete_rows_(table, rowfilter, printstr='all rows'): """Deletes rows in a data table based on a filter args: table (Spotfire.table): table to delete from rowfilter (Spotfire.Dxp.Data.rowFilter): filter to delete with """ from Spotfire.Dxp.Data import RowSelection heading = "REMOVE ROWS" message = 'Deleting {} from table {}'.format(printstr, table.Name) confirmation = yes_no_message(message, heading) if confirmation: table.RemoveRows(RowSelection(rowfilter)) else: heading = 'User chickened out' message = 'You decided not to delete!' ok_message(message, heading)
def rename_columns(doc, table_name, orig_names, new_names): """Renames selected columns in data table args: doc (Spotfire document instance): document to read from """ LOGGER.debug('--------------') LOGGER.debug(orig_names) LOGGER.debug(new_names) if orig_names and new_names: heading = 'Renaming columns in table {}!'.format(table_name) message = ('Do you want to rename' + ' {} to {}?').format(list_string(orig_names), list_string(new_names)) confirmation = yes_no_message(message, heading) if confirmation: table = get_table(doc, table_name) switch_dict = dict(zip(orig_names, new_names)) LOGGER.debug(switch_dict) for col in table.Columns: col_name = col.Name # This does not work in spotfire 10.. #LOGGER.debug('%s: %s', col_name, switch_dict[col_name]) if col_name in orig_names: try: new_name = switch_dict[col_name] col.Name = new_name LOGGER.debug('Moving %s to %s', col, new_name) except KeyError: LOGGER.warning('%s not in list', col) else: LOGGER.debug('Skipping %s', col_name) LOGGER.debug('Done with swithing') else: heading = 'Nothing to replace!!' message = 'Couldnt find anything to replace in rename columns' ok_message(message, heading)
def make_vector_and_well_tables(doc, in_table_name): """Makes tables with names of vectors and wells args doc (Spotfire document instance): document to read from in_table_name (str): name of table to extract from """ vector_names = get_vector_names(doc, in_table_name) well_names = get_well_names(doc, in_table_name) vector_table_name = 'Vectors' well_table_name = 'Wells' if in_table_name in [vector_table_name, well_table_name]: message = ('The name of the table to create well and vector names' + ' from corresponds to the name of one of the tables.\n ' + 'This is not a particularly clever idea, so try again with\n'+ ' a different name!') ok_message(message) else: make_table(doc, vector_table_name, vector_table_name , vector_names) make_table(doc, well_table_name, well_table_name , well_names)
def define_relation(doc, first_table_name, second_table_name, first_col_name, second_col_name=None, box_message=True): """Sets relation between table columns in spotfire project args: doc (Spotfire document instance): document to read from first_table_name (str): name of table second_table_name (str): name of table first_col_name (str): name of first column second_col_name (str or None): name of second column, is defaulted to None, if None, then it is assumed that the column name is common between the tables, and equal to first_col_name box_message (bool): decides if user will get message in pop up box """ from Spotfire.Dxp.Data import DataRelation if second_col_name is None: second_col_name = first_col_name tables = doc_tables(doc) # set the left and right tables try: first= tables[first_table_name] second= tables[second_table_name] # add the relation # "Region" is the column that relates the two tables relation_string = "[{}].[{}] = [{}].[{}]".format(first_table_name, first_col_name, second_table_name, second_col_name) doc.Data.Relations.Add(first, second, relation_string) except KeyError: message = ('Could not set up relation between {} in table {}\n' + ' and {} in table {}\n.' + 'Ensure that both tables exist').format(first_table_name, first_col_name, second_table_name, second_col_name) if box_message: ok_message(message)
def copy_table(doc, table_name, newname): """Copies data table in spotfire project args: doc (Spotfire document instance): document to read from table_name (str): name of table """ #from Spotfire.Dxp.Data import sourceTable from Spotfire.Dxp.Data.Import import DataTableDataSource table = get_table(doc, table_name) tablesource = DataTableDataSource(table) try: doc.Data.Tables.Add(newname, tablesource) LOGGER.debug('Copied table {} to {}'.format(table_name, newname)) except ValueError: message = ("Table {} already exists!" + " no copy will be made").format(newname) heading = "Warning!" ok_message(message, heading) except TypeError: table.ReplaceData(tablesource) LOGGER.debug("This did not work")