1515#
1616__author__ = 'bmiller'
1717
18- import re
18+ import re , os
1919from docutils import nodes
2020from docutils .parsers .rst import directives
2121from runestone .common .runestonedirective import RunestoneNode , RunestoneIdDirective , get_node_line
@@ -66,7 +66,6 @@ class SpreadSheet(RunestoneIdDirective):
6666 :fromcsv: path/to/csv/file
6767 :colwidths: list of column widths
6868 :coltitles: list of column names
69- :coltypes: list of column types
7069 :mindimensions: mincols, minrows -- minDimensions:[10,5]
7170
7271 A1,B1,C1,D1...
@@ -80,7 +79,7 @@ class SpreadSheet(RunestoneIdDirective):
8079 'fromcsv' : directives .unchanged ,
8180 'colwidths' : directives .unchanged ,
8281 'coltitles' : directives .unchanged ,
83- 'coltypes ' : directives .unchanged
82+ 'mindimensions ' : directives .unchanged
8483 })
8584
8685
@@ -96,18 +95,34 @@ def run(self):
9695 self .options ['asserts' ] = suffix
9796 self .options ['autograde' ] = 'data-autograde="true"'
9897 else :
98+ self .options ['asserts' ] = '""'
9999 self .options ['autograde' ] = ''
100100
101101 if 'fromcsv' in self .options :
102- self .content = self .body_from_csv (self .options ['fromcsv' ])
102+ self .content = self .body_from_csv (env , self .options ['fromcsv' ])
103103 else :
104104 if self .content :
105- self .content = self .body_to_csv ()
105+ self .content = self .body_to_csv (self . content )
106106 else :
107107 raise ValueError ("You must specify either from csv or provide content in the body" )
108108
109109 self .options ['data' ] = self .content
110110
111+ if 'coltitles' not in self .options :
112+ self .options ['coltitles' ] = ""
113+ else :
114+ self .options ['coltitles' ] = "data-coltitles=[{}]" .format (self .options ['coltitles' ])
115+
116+ if 'mindimensions' not in self .options :
117+ self .options ['mindimensions' ] = ""
118+ else :
119+ self .options ['mindimensions' ] = "data-mindimensions=[{}]" .format (self .options ['mindimensions' ])
120+
121+ if 'colwidths' not in self .options :
122+ self .options ['colwidths' ] = ""
123+ else :
124+ self .options ['colwidths' ] = "data-colwidths=[{}]" .format (self .options ['colwidths' ])
125+
111126 ssnode = SpreadSheetNode (self .options , rawsource = self .block_text )
112127 ssnode .source , ssnode .line = self .state_machine .get_source_and_line (self .lineno )
113128 self .add_name (ssnode ) # make this divid available as a target for :ref:
@@ -116,9 +131,9 @@ def run(self):
116131
117132
118133
119- def body_to_csv (self ):
134+ def body_to_csv (self , row_list ):
120135 csvlist = []
121- for row in self . content :
136+ for row in row_list :
122137 if re .match (r'^\s*====' , row ):
123138 break
124139 items = row .split (',' )
@@ -134,8 +149,16 @@ def body_to_csv(self):
134149 csvlist .append (ilist )
135150 return csvlist
136151
137- def body_from_csv (self , csvfile ):
138- return "to do"
152+ def body_from_csv (self , env , csvfile ):
153+ ffpath = os .path .dirname (self .srcpath )
154+ print (self .srcpath , os .getcwd ())
155+ filename = os .path .join (env .srcdir , ffpath , csvfile )
156+
157+ print ("\n \n PATH=" , self .srcpath )
158+ with open (filename ,'r' ) as csv :
159+ content = csv .readlines ()
160+
161+ return self .body_to_csv (content )
139162
140163def is_float (s ):
141164 try :
@@ -154,7 +177,7 @@ def as_int_or_float(s):
154177
155178
156179TEMPLATE = """
157- <div id="{divid}" data-component="spreadsheet" class="runestone" {autograde}>
180+ <div id="{divid}" data-component="spreadsheet" class="runestone" {autograde} {mindimensions} {colwidths} {coltitles} >
158181 <div id="{divid}_sheet"></div>
159182
160183 <script>
0 commit comments