def create_example1(tmpdir): tmpdir.join("setup.py").write( d( """ from setuptools import setup def main(): setup( name='example1', description='example1 project for testing detox', version='0.4', packages=['example1',], ) if __name__ == '__main__': main() """ ) ) tmpdir.join("tox.ini").write( d( """ [testenv:py] """ ) ) tmpdir.join("example1", "__init__.py").ensure()
def test_trivial(self): code = d("""\ a = 1 """) result = d("""\ a = 1 __livesource_listing[1].append(('a', a, )) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_format(self): code = d("""\ print "a{}".format("b") """) result = d("""\ print "a{}".format("b") __livesource_listing[1].append((None, " ".join(("a{}".format("b"),)),)) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_concatenate(self): code = d("""\ print "a" + "b" """) result = d("""\ print "a" + "b" __livesource_listing[1].append((None, " ".join(("a" + "b",)),)) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_swap(self): code = d("""\ a, b = b, a """) result = d("""\ a, b = b, a __livesource_listing[1].append(('a', a, )) __livesource_listing[1].append(('b', b, )) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_trivial(self): code = d("""\ if x: pass """) result = d("""\ if x: __livesource_listing[1].append((None, x,)) pass """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_attribute(self): code = d("""\ a.x = (2,) a.x.y = [1, 2] """) result = d("""\ a.x = (2,) __livesource_listing[1].append(('a.x', a.x, )) a.x.y = [1, 2] __livesource_listing[2].append(('a.x.y', a.x.y, )) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def test_failing_load_test(self): h = Host() orig_wd = h.getcwd() tmpdir = None try: tmpdir = h.mkdtemp() h.chdir(tmpdir) h.write_text_file( 'load_test.py', d("""\ import unittest def load_tests(_, _2, _3): assert False """)) test_set = TestSet(MockArgs()) test_set.parallel_tests = [TestInput('load_test.BaseTest.test_x')] r = Runner() r.args.jobs = 1 ret, _, trace = r.run(test_set) self.assertEqual(ret, 1) self.assertIn('BaseTest', trace['traceEvents'][0]['args']['err']) finally: h.chdir(orig_wd) if tmpdir: h.rmtree(tmpdir)
def test_failing_load_test(self): h = Host() orig_wd = h.getcwd() tmpdir = None try: tmpdir = h.mkdtemp() h.chdir(tmpdir) h.write_text_file('load_test.py', d("""\ import unittest def load_tests(_, _2, _3): assert False """)) test_set = TestSet() test_set.parallel_tests = [TestInput('load_test.BaseTest.test_x')] r = Runner() r.args.jobs = 1 ret, _, trace = r.run(test_set) self.assertEqual(ret, 1) self.assertIn('Failed to load "load_test.BaseTest.test_x" in ' 'run_one_test', trace['traceEvents'][0]['args']['err']) finally: h.chdir(orig_wd) if tmpdir: h.rmtree(tmpdir)
def dash_scatter(fig): external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) styles = {'pre': {'border': 'thin lightgrey solid', 'overflowX': 'scroll'}} app.layout = html.Div([ dcc.Graph(id='basic-interactions', figure=fig), html.Div([ dcc.Markdown( d(""" **Click Data** Click on points in the graph. """)), html.Pre(id='click-data', style=styles['pre']), ], className='three columns') ]) @app.callback(Output('click-data', 'children'), [Input('basic-interactions', 'clickData')]) def display_click_data(clickData): if clickData: print(clickData) image_filename = 'Capture4.PNG' # replace with your own image encoded_image = base64.b64encode(open(image_filename, 'rb').read()) return html.Img( src='data:image/png;base64,{}'.format(encoded_image.decode())) return json.dumps("rien", indent=2) app.run_server(debug=True)
def dash_scatter(fig, ecru, traite): external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) styles = {'pre': {'border': 'no border', 'overflowX': 'scroll'}} app.layout = html.Div([ dcc.Graph(id='basic-interactions', figure=fig), html.Div([ dcc.Markdown(d(""" **Defect Pictures** """)), html.Pre(id='click-data', style=styles['pre']), ], className='three columns') ]) @app.callback(Output('click-data', 'children'), [Input('basic-interactions', 'clickData')]) def display_click_data(clickData): if clickData: x = clickData["points"][0]["x"] y = clickData["points"][0]["y"] donnees = ecru.dataframe[ecru.dataframe["metrage"] == x] id_image = donnees["image"].iloc[0] id_roule = donnees["roule"].iloc[0] image_filename = 'Matching/' + str( id_roule) + "/Images/" + "d_26_" + str(id_image) + "_0.jpg" encoded_image = base64.b64encode(open(image_filename, 'rb').read()) return html.Img( src='data:image/png;base64,{}'.format(encoded_image.decode())) return "Cliquez sur les defauts sur le graph" app.run_server(debug=True)
def create_example2(tmpdir): tmpdir.join("tox.ini").write(d(""" [tox] skipsdist = True [testenv:py] """)) tmpdir.join("example2", "__init__.py").ensure()
def test_trivial(self): code = d("""\ a = 1 """) result = lambda x: collections.deque(x, maxlen=10) values = LiveSource(code).get_values() self.assertEqual(values[1], result([('a', 1)]))
def test_nested_message(self): """ Nested messages are represented as nested dictionaries. """ self.check({"dict": {"foo": "bar"}}, d(u'''\ dict { foo: "bar" } '''))
def _sanity_check_gif(generate_gif): if generate_gif and GIF_BACKGROUND_COLOR == 'transparent' and not GIF_ALLOW_TRANSPARENT: exit(d(""" ****************************************************************** WARNING: "transparent" not allowed by default as gif background, because generated files are 10x bigger. If you are really sure set GIF_ALLOW_TRANSPARENT to True in settings.py and rerun. ****************************************************************** """))
def test_multiline(self): code = d("""\ a = 1 b = 2 c = 3 """) result = d("""\ a = 1 __livesource_listing[1].append(('a', a, )) b = 2 __livesource_listing[2].append(('b', b, )) c = 3 __livesource_listing[3].append(('c', c, )) """) parsed_tree = ast.dump(LiveSource(code)._parse()) expected_tree = ast.dump(ast.parse(result)) self.assertEqual(parsed_tree, expected_tree)
def Home(): layout = html.Div(children=[ dcc.Markdown( d(""" # Hello! Welcome to my Covid-19 dashboard thingy. All data is downloaded from [here](https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data). """)), TimeseriesLayout, ]) return layout
def update_header(n_clicks): if n_clicks is None: n_sim = 1 else: n_sim = n_clicks + 1 return dcc.Markdown( d(""" **Simulation #{}:** """.format(n_sim)))
def create_example3(tmpdir): tmpdir.join("tox.ini").write(d(""" [tox] skipsdist = True [testenv] commands = python -c 'import time; time.sleep(1)' [testenv:py1] [testenv:py2] """)) tmpdir.join("example3", "__init__.py").ensure()
def test_real_unimportable_main(self): h = self.make_host() tmpdir = None orig_wd = h.getcwd() out = err = None out_str = err_str = '' try: tmpdir = h.mkdtemp() h.chdir(tmpdir) out = tempfile.NamedTemporaryFile(delete=False) err = tempfile.NamedTemporaryFile(delete=False) path_above_typ = h.realpath(h.dirname(__file__), '..', '..') env = h.env.copy() if 'PYTHONPATH' in env: # pragma: untested env['PYTHONPATH'] = '%s%s%s' % (env['PYTHONPATH'], h.pathsep, path_above_typ) else: # pragma: untested. env['PYTHONPATH'] = path_above_typ h.write_text_file( 'test', d(""" import sys import typ importable = typ.WinMultiprocessing.importable sys.exit(typ.main(win_multiprocessing=importable)) """)) h.stdout = out h.stderr = err ret = h.call_inline([h.python_interpreter, h.join(tmpdir, 'test')], env=env) finally: h.chdir(orig_wd) if tmpdir: h.rmtree(tmpdir) if out: out.close() out = open(out.name) out_str = out.read() out.close() h.remove(out.name) if err: err.close() err = open(err.name) err_str = err.read() err.close() h.remove(err.name) self.assertEqual(ret, 1) self.assertEqual(out_str, '') self.assertIn('ValueError: The __main__ module ', err_str)
def tab2_content(): return html.Div(children=[ html.Div(style={'width':'20%', 'display': 'inline-block', 'float':'left'},children=[ dcc.Markdown(d(""" Filters """)), ]), html.Div(style={'width':'60%', 'display': 'inline-block'},children=[ dcc.Markdown(d(""" **Hover Data** """)), html.Div(style={'display': 'inline','word-wrap': 'break-word'},children=issues_list_func(issues)) ]), html.Div(style={'width':'20%', 'display': 'inline-block', 'float':'right'},children=[ dcc.Markdown(d(""" ycvjgvhg """)), ]), ])
def test_real_unimportable_main(self): h = self.make_host() tmpdir = None orig_wd = h.getcwd() out = err = None out_str = err_str = '' try: tmpdir = h.mkdtemp() h.chdir(tmpdir) out = tempfile.NamedTemporaryFile(delete=False) err = tempfile.NamedTemporaryFile(delete=False) path_above_typ = h.realpath(h.dirname(__file__), '..', '..') env = h.env.copy() if 'PYTHONPATH' in env: # pragma: untested env['PYTHONPATH'] = '%s%s%s' % (env['PYTHONPATH'], h.pathsep, path_above_typ) else: # pragma: untested. env['PYTHONPATH'] = path_above_typ h.write_text_file('test', d(""" import sys import typ importable = typ.WinMultiprocessing.importable sys.exit(typ.main(win_multiprocessing=importable)) """)) h.stdout = out h.stderr = err ret = h.call_inline([h.python_interpreter, h.join(tmpdir, 'test')], env=env) finally: h.chdir(orig_wd) if tmpdir: h.rmtree(tmpdir) if out: out.close() out = open(out.name) out_str = out.read() out.close() h.remove(out.name) if err: err.close() err = open(err.name) err_str = err.read() err.close() h.remove(err.name) self.assertEqual(ret, 1) self.assertEqual(out_str, '') self.assertIn('ValueError: The __main__ module ', err_str)
def setup_cli(): """Set up command line options and help text""" global __doc__ prog_usage="""\ mk_dirs.py [options] base_directory """ parser = OptionParser(description=d(__doc__), usage=d(prog_usage)) ''' We don't want this for now. It's the default behavior ***** help_txt = """\ Group directories by day. Creates directories in the form of mm-dd-yyyy and puts existing files into the proper daily directory. Will create directories if they do not already exist. Will place files in month based sub-directories if they exist, otherwise it will create a daily directly under the base directory (see -m option). This is the default action. """ parser.add_option('', '--daily', help=d(help_txt), action='store_true', dest='day_action', default=True) ''' help_txt = """\ Group daily directories by month. Does not impact individual files Creates directories in the form of yyyy-mm and puts existing daily sub-directories in the form of dd-mm-yyyy under the proper month. Will create monthly directories if they do not already exist. Does not move individual files. """ parser.add_option('-m', '--monthly', help=d(help_txt), action='store_true', dest='month_action', default=False) help_txt = """\ Log every action on every remote file to std-out. """ parser.add_option('-v', '--verbose', help=d(help_txt), action='store_true', dest='verbose', default=False) help_txt = """\ Surpress all output. """ parser.add_option('-q', '--quiet', help=d(help_txt), action='store_true', default=False, dest='quiet') help_txt = """\ Debug level messages. Assumes -v and overrides -q. """ parser.add_option('-d', '--debug', help=d(help_txt), action='store_true', dest='debug', default=False) help_txt = """\ Simulate. Do not perform moves, just log what the program would do (Implies -v). """ parser.add_option('-s', '--simulate', help=d(help_txt), action='store_true', dest='simulate', default=False) return parser
def test_basic(self): person = Person() person.name = "John Doe" person.birthday.year = 1970 person.birthday.month = 1 person.birthday.day = 1 self.check(person, d('''\ name: "John Doe" birthday { year: 1970 month: 1 day: 1 } '''))
def build_dash_microservice(data): app = dash.Dash() app.layout = html.Div([ html.H1('CURRENCY PLOT'), dcc.Graph(id="basic-interactions", figure=data, config={"editable": True}, style={ "height": "100vh", "width": "100%" }), # this HTML Div creates the data div that gets lasso'd or rectangled html.Div( className='row', children=[ html.Div( [ dcc.Markdown( d(""" **Selection Data** Choose the lasso or rectangle tool in the graph's menu bar and then select points in the graph. Note that if `layout.clickmode = 'event+select'`, selection data also accumulates (or un-accumulates) selected data if you hold down the shift button while clicking. """)), html.Pre(id='selected-data'), # style=styles['pre']), ], className='three columns') ]) ]) # basic-interactions is a component and selectedData is a property # The input is the basic-interactions plot, and the selectedData is the pre made prop that holds the # selected data that was highlighted with the lasso. You can see selectedData prop in the redux store # the ouptut is where we put the input @app.callback(Output('selected-data', 'children'), [Input('basic-interactions', 'selectedData')]) def display_selected_data(selectedData): with open('calculations/data/selectedData', 'w') as f: # writing JSON object json.dump(selectedData, f) #return json.dumps(selectedData, indent=2) app.run_server()
def test_main(c): '''loadconfig main unittest''' host = 'leon' conf_option = '-E="{}"'.format(c.conf) with capture_stream() as stdout: main([c.prog, conf_option, host]) exp = d('''\ export ARGS="" export DOCKER__IMAGE="reg.gdl/debian" export HOST="leon" export PROG="test_loadconfig.py" export SYSTEM_PATH="/data/salt/system" export VERSION="0.1.7"''') ret = '\n'.join(sorted(stdout.getvalue()[:-1].split('\n'))) assert exp == ret
def Update_WaterFigureTitle(selectedData, value): para_names = [ '溫度', '導電度(mS/cm)', '氧化還原電位(mV, ORP)', '溶氧量(mg/L, DO)', '溶氧度(%)', '濁度(NTU)', '酸鹼值(pH)', '鹽度(ppt)', '總固形物(g/L, TDS)', '水深' ] V = para_names[value] if not selectedData: idl = [1] else: idl = [int(i['hovertext']) for i in selectedData['points']] dd = str(idl) text = d(f""" {V}: 包含樣站: {dd[1:-1]} """) return text
def test_converter(self): from datetime import date @protobuf_extra.converter(date) def date_to_dict(date_instance): """ A function that takes a datetime.date instance, and returns a test.Person_pb2.Date compatible Python dictionary. """ return {"year": date_instance.year, "month": date_instance.month, "day": date_instance.day} self.check({'birthday': date(2013, 10, 5)}, d(u"""\ birthday { day: 5 month: 10 year: 2013 } """)) # just to clear the state del protobuf_extra.fromPythonConverters[0]
def test_unsuccessful_map_reduce(self, tracer, client): db_name = self.random_string() collection_name = self.random_string() collection = client[db_name][collection_name] docs = [dict(items=[1, 2, 3]), dict(items=[2, 3]), dict(items=[3])] collection.insert_many(docs) tracer.reset() mapper = Code( d(''' function() { this.items.forEach(function(i) { throw new Error('Bomb!'); }); } ''')) reducer = Code('function (key, values) { }') with pytest.raises(OperationFailure): collection.map_reduce(mapper, reducer, 'results') spans = tracer.finished_spans() assert len(spans) == 1 span = spans[0] assert span.operation_name == 'mapreduce' assert span.tags['namespace'] == self.namespace( db_name, collection_name) assert span.tags['custom'] == 'tag' assert span.tags['command.name'] == 'mapreduce' assert span.tags[tags.COMPONENT] == 'PyMongo' assert span.tags[tags.DATABASE_INSTANCE] == db_name assert span.tags['reported_duration'] assert span.tags[tags.ERROR] is True assert span.tags['sfx.error.kind'] == 'JSInterpreterFailure' assert 'Error: Bomb!' in span.tags['sfx.error.message'] failure = json.loads(span.tags['event.failure']) assert failure['code'] == 139 assert failure['codeName'] == 'JSInterpreterFailure' assert failure['ok'] == 0.0 assert 'Error: Bomb!' in failure['errmsg']
def update_header(n_clicks): # if n_clicks is None: # return dcc.Markdown(d(""" # **Click the button** # to show a simulation. # """)) # else: # return dcc.Markdown(d(""" # **Simulation #{}:** # Node color show proportion of ancestral material. # """.format(n_clicks))) if n_clicks is None: n_sim = 1 else: n_sim = n_clicks + 1 return dcc.Markdown( d(""" **Simulation #{}:** """.format(n_sim)))
'mode': 'markers', 'marker': { 'size': 12 } }], 'layout': { 'clickmode': 'event+select' } }), html.Div(className='row', children=[ html.Div([ dcc.Markdown( d(''' **Hover Data** Mouse over values in the graph. ''')), html.Pre(id='hover-data', style=styles['pre']) ], className='three columns'), html.Div([ dcc.Markdown( d(""" **Click Data** Click on points in the graph. """)), html.Pre(id='click-data', style=styles['pre']) ], className='three columns'),
def Visualise_on_Local_Host( nodes, node_title, data_x, data_y, z_column, embeddings_x=None, embeddings_y=None, labels='', edges='', edge_influential='', edge_source='', edge_target='', scale_by=2, main_article_value=0 ): ###################################################################################################################################################################### # styles: for right side hover/click component styles={ 'pre': { 'border': 'thin lightgrey solid', 'overflowX': 'scroll' } } app.layout=html.Div([ #########################Title html.Div([html.H1("Journal Vis")], className="row", style={'textAlign': "center"}), #############################################################################################define the row html.Div( className="row", children=[ ############################################middle graph component html.Div( className="eight columns", children=[dcc.Graph(id="my-graph", figure=Visualise_3D_Network(nodes, node_title, data_x, data_y, z_column, labels=labels ))], ), #########################################right side two output component html.Div( className="two columns", children=[ html.Div( className='twelve columns', children=[ dcc.Markdown(d(""" **Hover Data** Hover over the nodes to see key information. """)), html.Pre(id='hover-data', style=styles['pre']) ], style={'height': '400px'}), html.Div( className='twelve columns', children=[ dcc.Markdown(d(""" **Click Data** Click on journals in the graph for more information. """)), html.Pre(id='click-data', style=styles['pre']) ], style={'height': '400px'}) ] ) ] ) ]) ################################### callback for additional components # @app.callback( # dash.dependencies.Output('my-graph', 'figure')) # def update_output(): # # return Visualise_Network_3D("test.csv", # 'Title', # 'Outliers_x', # 'Outliers_y', # 'Year', # 'Clustered_x', # 'Clustered_y', # 'Year', # 'Clustered_labels', # "Data/node.csv", # 'Influential', # 'Source', # 'Target' # ) # # to update the global variable of YEAR and ACCOUNT ################################ callback for right side components @app.callback( dash.dependencies.Output('hover-data', 'children'), [dash.dependencies.Input('my-graph', 'hoverData')]) def display_hover_data(hoverData): return json.dumps(hoverData, indent=2) @app.callback( dash.dependencies.Output('click-data', 'children'), [dash.dependencies.Input('my-graph', 'clickData')]) def display_click_data(clickData): return json.dumps(clickData, indent=2) if __name__ == '__main__': app.run_server(debug=True)
'text': ['w', 'x', 'y', 'z'], 'customdata': ['c.w', 'c.x', 'c.y', 'c.z'], 'name': 'Trace 2', 'mode': 'markers', 'marker': { 'size': 12 } }] }), html.Div(className='row', children=[ html.Div([ dcc.Markdown( d(""" **Hover Data** Mouse over values in the graph. """)), html.Pre(id='hover-data', style=styles['pre']) ], className='three columns'), html.Div([ dcc.Markdown( d(""" **Click Data** Click on points in the graph. """)), html.Pre(id='click-data', style=styles['pre']), ], className='three columns'),
"label": str(n) + ":00", "value": str(n), } for n in range(24)], multi=True, placeholder="Select certain hours", ) ], ), ]), html.Div(className='row', children=[ dcc.Graph(id="taxi_map"), html.Div([ dcc.Markdown( d(""" Click on the map to see details here!. """)), html.Pre(id='map_click-data', style=styles['pre']), ], className='three columns'), html.Div([ dcc.Markdown( d(""" Select points on the map to see details here! """)), html.Pre(id='map_selected-data', style=styles['pre']), ], className='three columns'), html.Div([ dcc.Markdown( d("""
} }] } }, config={ 'editable': True, 'edits': { 'shapePosition': True } }), html.Div(className='six columns', children=[ html.Div([ dcc.Markdown( d(""" **Zoom and Relayout Data** """)), html.Pre(id='relayout-data', style=styles['pre']), ]) ]) ]) @app.callback(Output('relayout-data', 'children'), [Input('basic-interactions', 'relayoutData')]) def display_selected_data(relayoutData): return json.dumps(relayoutData, indent=2) if __name__ == '__main__':
import sys from textwrap import dedent as d from simplexml import parsestring # Globals. URL = ('http://developer.multimap.com/API/geocode/1.2/OA10110913592184095' '?countryCode=DK&postalCode=%d') # Parse options. try: zipcode = int(sys.argv[1]) if zipcode < 1000 or zipcode > 9999: raise ValueError except IndexError: sys.exit(d("""\ Outputs longitude and latitude for a danish zipcode. Usage: multimap-danish-zipcode-search.py [ZIPCODE]""")) except ValueError: sys.exit("[ZIPCODE] must be in the range 1000-9999") # Get response from multimap api. response = urllib2.urlopen(URL % zipcode).read()\ .replace('"http://clients.multimap.com/API"', '""').strip() # Parse xml and write longitude and latitude. xml = parsestring(response) try: print '%s %s' % (xml.Location.Point.Lat, xml.Location.Point.Lon) except KeyError: pass # Do nothing on error.
def test_template(self): """ Ability to create a template ASCII representation of a Message subclass. Contrast this with the output of MessageToDictionary(Person). Scalar types have the emptyish default value, but repeated scalars don't appear at all since there is no ASCII representation for an empty list. """ self.maxDiff = None self.check(Person, d('''\ age: 0 birthday { day: 0 month: 0 year: 0 } children { age: 0 birthday { day: 0 month: 0 year: 0 } d: 0.0 data: "" f: 0.0 f32: 0 f64: 0 flag: false gender: 0 i32: 0 i64: 0 is_married: true name: "" nationality: 0 s32: 0 s64: 0 sf32: 0 sf64: 0 text: "" u32: 0 u64: 0 } d: 0.0 data: "" f: 0.0 f32: 0 f64: 0 flag: false gender: 0 i32: 0 i64: 0 is_married: true name: "" nationality: 0 s32: 0 s64: 0 sf32: 0 sf64: 0 text: "" u32: 0 u64: 0 '''))
def create_animation( filename, generate_svg=True, generate_js_svg=False, generate_gif=False, ): _sanity_check_gif(generate_gif) filename_noext = re.sub(r'\.[^\.]+$','',filename) filename_noext_ascii = re.sub(r'\\([\\u])','\\1', json.dumps(filename_noext))[1:-1] baseid = basename(filename_noext_ascii) # load xml doc = etree.parse(filename, parser) # for xlink namespace introduction doc.getroot().set('{http://www.w3.org/1999/xlink}used','') #clear all extra elements this program may have previously added for el in doc.xpath("/n:svg/n:style", namespaces=namespaces): if re.match( r'-Kanimaji$', g.get('id') ): doc.getroot().remove(el) for g in doc.xpath("/n:svg/n:g", namespaces=namespaces): if re.match( r'-Kanimaji$', g.get('id') ): doc.getroot().remove(g) # create groups with a copies (references actually) of the paths bg_g = E.g(id = 'kvg:'+baseid+'-bg-Kanimaji', style = ('fill:none;stroke:%s;stroke-width:%f;'+ 'stroke-linecap:round;stroke-linejoin:round;') % (STOKE_UNFILLED_COLOR, STOKE_UNFILLED_WIDTH) ) anim_g = E.g(id = 'kvg:'+baseid+'-anim-Kanimaji', style = ('fill:none;stroke:%s;stroke-width:%f;'+ 'stroke-linecap:round;stroke-linejoin:round;') % (STOKE_FILLED_COLOR, STOKE_FILLED_WIDTH) ) if SHOW_BRUSH: brush_g = E.g(id = 'kvg:'+baseid+'-brush-Kanimaji', style = ('fill:none;stroke:%s;stroke-width:%f;'+ 'stroke-linecap:round;stroke-linejoin:round;') % (BRUSH_COLOR, BRUSH_WIDTH)) brush_brd_g = E.g(id = 'kvg:'+baseid+'-brush-brd-Kanimaji', style = ('fill:none;stroke:%s;stroke-width:%f;'+ 'stroke-linecap:round;stroke-linejoin:round;') % (BRUSH_BORDER_COLOR, BRUSH_BORDER_WIDTH)) # compute total length and time, at first totlen = 0 tottime = 0 for g in doc.xpath("/n:svg/n:g", namespaces=namespaces): if re.match( r'^kvg:StrokeNumbers_', g.get('id') ): continue for p in g.xpath(".//n:path", namespaces=namespaces): pathlen = compute_path_len(p.get('d')) duration = stroke_length_to_duration(pathlen) totlen += pathlen tottime += duration animation_time = time_rescale(tottime) #math.pow(3 * tottime, 2.0/3) tottime += WAIT_AFTER * tottime / animation_time actual_animation_time = animation_time animation_time += WAIT_AFTER css_header = d(""" /* CSS automatically generated by kanimaji.py, do not edit! */ """) if generate_svg: animated_css = css_header if generate_js_svg: js_animated_css = css_header + d(""" .backward { animation-direction: reverse !important; } """) js_anim_els = [] # collect the ids of animating elements js_anim_time = [] # the time set (as default) for each animation if generate_gif: static_css = {} last_frame_index = int(actual_animation_time/GIF_FRAME_DURATION)+1 for i in range(0, last_frame_index+1): static_css[i] = css_header last_frame_delay = animation_time - last_frame_index*GIF_FRAME_DURATION elapsedlen = 0 elapsedtime = 0 # add css elements for all strokes for g in doc.xpath("/n:svg/n:g", namespaces=namespaces): groupid = g.get('id') if re.match( r'^kvg:StrokeNumbers_', groupid ): rule = d(""" #%s { display: none; }""" % re.sub(r':', '\\\\3a ', groupid)) if generate_svg: animated_css += rule if generate_js_svg: js_animated_css += rule if generate_gif: for k in static_css: static_css[k] += rule continue gidcss = re.sub(r':', '\\\\3a ', groupid) rule = d(""" #%s { stroke-width: %.01fpx !important; stroke: %s !important; }""" % (gidcss, STOKE_BORDER_WIDTH, STOKE_BORDER_COLOR)) if generate_svg: animated_css += rule if generate_js_svg: js_animated_css += rule if generate_gif: for k in static_css: static_css[k] += rule for p in g.xpath(".//n:path", namespaces=namespaces): pathid = p.get('id') pathidcss = re.sub(r':', '\\\\3a ', pathid) if generate_js_svg: js_anim_els.append({}) bg_pathid = pathid+'-bg' bg_pathidcss = pathidcss+'-bg' ref = E.use(id = bg_pathid) ref.set('{http://www.w3.org/1999/xlink}href','#'+pathid) bg_g.append(ref) if generate_js_svg: js_anim_els[-1]["bg"] = ref anim_pathid = pathid+'-anim' anim_pathidcss = pathidcss+'-anim' ref = E.use(id = anim_pathid) ref.set('{http://www.w3.org/1999/xlink}href','#'+pathid) anim_g.append(ref) if generate_js_svg: js_anim_els[-1]["anim"] = ref if SHOW_BRUSH: brush_pathid = pathid+'-brush' brush_pathidcss = pathidcss+'-brush' ref = E.use(id = brush_pathid) ref.set('{http://www.w3.org/1999/xlink}href','#'+pathid) brush_g.append(ref) if generate_js_svg: js_anim_els[-1]["brush"] = ref brush_brd_pathid = pathid+'-brush-brd' brush_brd_pathidcss = pathidcss+'-brush-brd' ref = E.use(id = brush_brd_pathid) ref.set('{http://www.w3.org/1999/xlink}href','#'+pathid) brush_brd_g.append(ref) if generate_js_svg: js_anim_els[-1]["brush-brd"] = ref pathname = re.sub(r'^kvg:','',pathid) pathlen = compute_path_len(p.get('d')) duration = stroke_length_to_duration(pathlen) relduration = duration * tottime / animation_time # unscaled time if generate_js_svg: js_anim_time.append(relduration) newelapsedlen = elapsedlen + pathlen newelapsedtime = elapsedtime + duration anim_start = elapsedtime/tottime*100 anim_end = newelapsedtime/tottime*100 if generate_svg: # animation stroke progression animated_css += d(""" @keyframes strike-%s { 0%% { stroke-dashoffset: %.03f; } %.03f%% { stroke-dashoffset: %.03f; } %.03f%% { stroke-dashoffset: 0; } 100%% { stroke-dashoffset: 0; } }""" % (pathname, pathlen, anim_start, pathlen, anim_end)) # animation visibility animated_css += d(""" @keyframes showhide-%s { %.03f%% { visibility: hidden; } %.03f%% { stroke: %s; } }""" % (pathname, anim_start, anim_end, STOKE_FILLING_COLOR)) # animation progression animated_css += d(""" #%s { stroke-dasharray: %.03f %.03f; stroke-dashoffset: 0; animation: strike-%s %.03fs %s infinite, showhide-%s %.03fs step-start infinite; }""" % (anim_pathidcss, pathlen, pathlen, pathname, animation_time, TIMING_FUNCTION, pathname, animation_time)) if SHOW_BRUSH: # brush element visibility animated_css += d(""" @keyframes showhide-brush-%s { %.03f%% { visibility: hidden; } %.03f%% { visibility: visible; } 100%% { visibility: hidden; } }""" % (pathname, anim_start, anim_end)) # brush element progression animated_css += d(""" #%s, #%s { stroke-dasharray: 0 %.03f; animation: strike-%s %.03fs %s infinite, showhide-brush-%s %.03fs step-start infinite; }""" % (brush_pathidcss, brush_brd_pathidcss, pathlen, pathname, animation_time, TIMING_FUNCTION, pathname, animation_time)) if generate_js_svg: js_animated_css += d("""\n /* stroke %s */""" % pathid) # brush and background hidden by default if SHOW_BRUSH: js_animated_css += d(""" #%s, #%s, #%s { visibility: hidden; }""") % (brush_pathidcss, brush_brd_pathidcss, bg_pathidcss) # hide stroke after current element after_curr = '[class *= "current"]' js_animated_css += d(""" %s ~ #%s { visibility: hidden; }""") % (after_curr, anim_pathidcss) # and show bg after current element, or if animated js_animated_css += d(""" %s ~ #%s, #%s.animate { visibility: visible; }""") % (after_curr, bg_pathidcss, bg_pathidcss) # animation stroke progression js_animated_css += d(""" @keyframes strike-%s { 0%% { stroke-dashoffset: %.03f; } 100%% { stroke-dashoffset: 0; } }""" % (pathname, pathlen)) js_animated_css += d(""" #%s.animate { stroke: %s; stroke-dasharray: %.03f %.03f; visibility: visible; animation: strike-%s %.03fs %s forwards 1; }""" % (anim_pathidcss, STOKE_FILLING_COLOR, pathlen, pathlen, pathname, relduration, TIMING_FUNCTION)) if SHOW_BRUSH: js_animated_css += d(""" @keyframes strike-brush-%s { 0%% { stroke-dashoffset: %.03f; } 100%% { stroke-dashoffset: 0.4; } }""" % (pathname, pathlen)) js_animated_css += d(""" #%s.animate.brush, #%s.animate.brush { stroke-dasharray: 0 %.03f; visibility: visible; animation: strike-brush-%s %.03fs %s forwards 1; }""") % (brush_pathidcss, brush_brd_pathidcss, pathlen, pathname, relduration, TIMING_FUNCTION) if generate_gif: for k in static_css: time = k * GIF_FRAME_DURATION reltime = time * tottime / animation_time # unscaled time static_css[k] += d(""" /* stroke %s */ """ % pathid) # animation if reltime < elapsedtime: #just hide everything rule = "#%s" % anim_pathidcss if SHOW_BRUSH: rule += ", #%s, #%s" % (brush_pathidcss, brush_brd_pathidcss) static_css[k] += d(""" %s { visibility: hidden; }""" % rule) elif reltime > newelapsedtime: #just hide the brush, and bg rule = "#%s" % bg_pathidcss if SHOW_BRUSH: rule += ", #%s, #%s" % (brush_pathidcss, brush_brd_pathidcss) static_css[k] += d(""" %s { visibility: hidden; }""" % (rule)) else: intervalprop = ((reltime-elapsedtime) / (newelapsedtime-elapsedtime)) progression = my_timing_func(intervalprop) static_css[k] += d(""" #%s { stroke-dasharray: %.03f %.03f; stroke-dashoffset: %.04f; stroke: %s; }""" % (anim_pathidcss, pathlen, pathlen+0.002, pathlen * (1-progression)+0.0015, STOKE_FILLING_COLOR)) if SHOW_BRUSH: static_css[k] += d(""" #%s, #%s { stroke-dasharray: 0.001 %.03f; stroke-dashoffset: %.04f; }""" % (brush_pathidcss, brush_brd_pathidcss, pathlen+0.002, pathlen * (1-progression)+0.0015)) elapsedlen = newelapsedlen elapsedtime = newelapsedtime # insert groups if SHOW_BRUSH and not SHOW_BRUSH_FRONT_BORDER: doc.getroot().append(brush_brd_g) doc.getroot().append(bg_g) if SHOW_BRUSH and SHOW_BRUSH_FRONT_BORDER: doc.getroot().append(brush_brd_g) doc.getroot().append(anim_g) if SHOW_BRUSH: doc.getroot().append(brush_g) if generate_svg: style = E.style(animated_css, id="style-Kanimaji") doc.getroot().insert(0, style) svgfile = filename_noext + '_anim.svg' output_dir = os.path.join(OUTPUT_DIR, 'svg') try: os.makedirs(output_dir) except OSError: pass output_path = os.path.join(output_dir, os.path.basename(svgfile)) doc.write(output_path, pretty_print=True) doc.getroot().remove(style) if generate_gif: svgframefiles = [] pngframefiles = [] svgexport_data = [] for k in static_css: svgframefile = filename_noext_ascii + ("_frame%04d.svg"%k) pngframefile = filename_noext_ascii + ("_frame%04d.png"%k) svgframefiles.append(svgframefile) pngframefiles.append(pngframefile) svgexport_data.append({"input": [abspath(svgframefile)], "output": [[abspath(pngframefile), "%d:%d"% (GIF_SIZE, GIF_SIZE)]]}) style = E.style(static_css[k], id="style-Kanimaji") doc.getroot().insert(0, style) output_dir = os.path.join(OUTPUT_DIR, 'gif') try: os.makedirs(output_dir) except OSError: pass output_path = os.path.join(output_dir, os.path.basename(svgframefile)) doc.write(output_path, pretty_print=True) doc.getroot().remove(style) # create json file svgexport_datafile = filename_noext_ascii+"_export_data.json" with open(svgexport_datafile,'w') as f: f.write(json.dumps(svgexport_data)) print 'created instructions %s' % svgexport_datafile # run svgexport cmdline = 'svgexport %s' % shescape(svgexport_datafile) print cmdline if os.system(cmdline) != 0: exit('Error running external command') if DELETE_TEMPORARY_FILES: os.remove(svgexport_datafile) for f in svgframefiles: os.remove(f) # generate GIF giffile_tmp1 = filename_noext + '_anim_tmp1.gif' giffile_tmp2 = filename_noext + '_anim_tmp2.gif' giffile = filename_noext + '_anim.gif' escpngframefiles = ' '.join(shescape(f) for f in pngframefiles[0:-1]) if GIF_BACKGROUND_COLOR == 'transparent': bgopts = '-dispose previous' else: bgopts = "-background '%s' -alpha remove" % GIF_BACKGROUND_COLOR cmdline = ("convert -delay %d %s -delay %d %s "+ "%s -layers OptimizePlus %s") % ( int(GIF_FRAME_DURATION*100), escpngframefiles, int(last_frame_delay*100), shescape(pngframefiles[-1]), bgopts, shescape(giffile_tmp1)) print cmdline if os.system(cmdline) != 0: exit('Error running external command') if DELETE_TEMPORARY_FILES: for f in pngframefiles: os.remove(f) print 'cleaned up.' cmdline = ("convert %s \\( -clone 0--1 -background none "+ "+append -quantize transparent -colors 63 "+ "-unique-colors -write mpr:cmap +delete \\) "+ "-map mpr:cmap %s") % ( shescape(giffile_tmp1), shescape(giffile_tmp2)) print cmdline if os.system(cmdline) != 0: exit('Error running external command') if DELETE_TEMPORARY_FILES: os.remove(giffile_tmp1) cmdline = ("gifsicle -O3 %s -o %s") % ( shescape(giffile_tmp2), shescape(giffile)) print cmdline if os.system(cmdline) != 0: exit('Error running external command') if DELETE_TEMPORARY_FILES: os.remove(giffile_tmp2) if generate_js_svg: f0insert = [bg_g, anim_g] if SHOW_BRUSH: f0insert += [brush_g, brush_brd_g] for g in f0insert: el = E.a() el.set("data-stroke","0") g.insert(0, el) for i in range(0, len(js_anim_els)): els = js_anim_els[i] for k in els: els[k].set("data-stroke",str(i+1)) els["anim"].set("data-duration", str(js_anim_time[i])) doc.getroot().set('data-num-strokes', str(len(js_anim_els))) style = E.style(js_animated_css, id="style-Kanimaji") doc.getroot().insert(0, style) svgfile = filename_noext + '_js_anim.svg' output_dir = os.path.join(OUTPUT_DIR, 'js_svg') try: os.makedirs(output_dir) except OSError: pass output_path = os.path.join(output_dir, os.path.basename(svgfile)) doc.write(output_path, pretty_print=True) doc.getroot().remove(style)
'x': df['date_super'], 'text': df['STRCITY'], 'customdata': df['storenum'], 'name': 'Super Date', 'type': 'histogram' } ], 'layout': {} } ), html.Div(className='row', children=[ html.Div([ dcc.Markdown(d(""" **Hover Data** Mouse over values in the graph. """)), html.Pre(id='hover-data', style=styles['pre']) ], className='three columns'), html.Div([ dcc.Markdown(d(""" **Click Data** Click on points in the graph. """)), html.Pre(id='click-data', style=styles['pre']), ], className='three columns'), html.Div([
def main(**kwargs): """ Command Line Interface (CLI) ============================ $ pb --type test.Person.Person --out ascii $ pb --type test.Person.Person --out dict $ pb --type test.Person.Person --out bin --partial >person.gpb $ pb --type test.Person.Person --in ascii --out ascii <person.txt $ pb --type test.Person.Person --in ascii --out dict <person.txt $ pb --type test.Person.Person --in ascii --out bin <person.txt >person.gpb $ pb --type test.Person.Person --in bin --out ascii <person.gpb $ pb --type test.Person.Person --in bin --out dict <person.gpb $ pb --type test.Person.Person --in bin --out bin --partial <person.gpg >person.gpb See 'pb --help' for more details. """ from argparse import ArgumentParser, RawDescriptionHelpFormatter from textwrap import dedent as d from sys import stdin, stdout, stderr from os import fdopen cli = ArgumentParser(description=d(''' Translates Google Protocol Buffer Messages into different representation. The '--type' (or '-t') is the only required option, and defines which message type to deal with. The message type must be prefixed with the basename of the file where the message type is defined. '''), epilog=d('''\ Options and their arguments can be written with or without equals sign between them. I.e. all of these are the same: '-i ascii', '-i=ascii', '--in ascii', '--in=ascii'. Input is read from standard input, and output goes to standard output. If no input format is specified, then input is ignored and the message type is used to generate the output. This is useful for generating a template, for example. The default output format is 'ascii'. For more details, see the module documentation ('pydoc ./pb'). '''), formatter_class=RawDescriptionHelpFormatter) cli.add_argument('-t', '--type', metavar='TYPE', required=True, help='Message type, e.g. act_samples.PbActivity.') cli.add_argument('-i', '--in', metavar='FORM', default=None, choices=('ascii','bin'), dest='input_format', help='Input format. One of "ascii" or "bin".') cli.add_argument('-o', '--out', metavar='FORM', default='ascii', choices=('ascii','bin','dict'), dest='output_format', help='Output format. One of "ascii", "bin", or "dict".') cli.add_argument('-p', '--partial', action='store_true', help='Whether to allow incomplete binary output.') args = cli.parse_args() # args.type should be either 'my.package.file_pb2.MyMessage' or 'my.package.file.MyMessage'. moduleName = '.'.join([v for v in kwargs.get("modulePrefix","").split('.') if v] + [v for v in args.type.split('.')[0:-1] if v]) if not moduleName.endswith("_pb2"): moduleName += "_pb2" messageName = args.type.split('.')[-1] try: module = __import__(moduleName, globals(), locals(), [messageName]) except ImportError: stderr.write("Unable to find module '%s'\n" % moduleName) exit(-1) try: MessageClass = getattr(module, messageName) except AttributeError: stderr.write("Module does not contain Message type '%s'\n" % messageName) exit(-1) if args.input_format is None: message = MessageClass elif args.input_format == 'ascii': message = MessageFromString(MessageClass, stdin.read()) elif args.input_format == 'bin': message = MessageFromBinary(MessageClass, stdin.read()) if args.output_format == 'ascii': print MessageToString(message) elif args.output_format == 'dict': print MessageToDictionary(message) elif args.output_format == 'bin': # Reopen stdout (fd=1) in binary mode. Also, use write() # instead of print. These together should bypass the Python's # helpful universal newline feature. stdout = fdopen(1, "wb") stdout.write(MessageToBinary(message, partial=args.partial))
'doubleClick': 'reset' } ), dcc.Graph( id = 'graph-2', figure = f, config = { 'doubleClick': 'reset' } ), html.Div(id='intermediate-value', style={'display': 'none'}), html.Div([ dcc.Markdown(d(""" **Zoom and Relayout Data** Click and drag on the graph to zoom or click on the zoom buttons in the graph's menu bar. Clicking on legend items will also fire this event. """)), html.Pre(id='relayout-data', style=styles['pre']), ], className='three columns') ]) #Function to create new datashader image, used in newPlotlyGeneration def newGraphplot(nodes, edges, name="", canvas=None, cat=None, x_range=None, y_range=None): if canvas is None: xr = x_range yr = y_range canvas = ds.Canvas(x_range=xr, y_range=yr, **cvsopts) #print(x_range)
'height': 1000, }, figure={'layout': { 'clickmode': 'event+select' }}, ), ], className="eight columns"), html.Div( [ html.H2('Control pannel'), html.Button('Delete Last element', id='button'), html.Button('Save Data', id='save'), html.Button('Plot Lines', id='plot_lines'), html.H2('Click Data'), dcc.Markdown(d("""Click on points in the graph.""")), html.Div(id='container-button-basic', children='-'), # html.Div([ # dash_table.DataTable( # id='editable_table', # columns=([{'id': 'fiducial', 'name': 'Fiducials'}]), # data=[{"fiducial": 0}], # editable=True # ), # ]), html.Pre(id='click-data', style={ 'border': 'thin lightgrey solid', 'overflowX': 'scroll' }), dcc.Input(id="from", type="text", placeholder="from"),
app.run_server(debug=True) app.layout = html.Div([ html.Div([html.H1("Transaction Network Graph")], className="row", style={'textAlign': "center"}), html.Div( className="row", children=[ html.Div( className="two columns", children=[ dcc.Markdown(d(""" **Time Range To Visualize** Slide the bar to define year range. """)), html.Div( className="twelve columns", children=[ dcc.RangeSlider( id='my-range-slider', min=2010, max=2019, step=1, value=[2010, 2019], marks={ 2010: {'label': '2010'}, 2011: {'label': '2011'}, 2012: {'label': '2012'}, 2013: {'label': '2013'},
}, config={ 'editable': True, 'edits': { 'shapePosition': True } } ), html.Div( className='six columns', children=[ html.Div( [ dcc.Markdown( d(""" **Zoom and Relayout Data** """)), html.Pre(id='relayout-data', style=styles['pre']), ] ) ] ) ]) @app.callback( Output('relayout-data', 'children'), [Input('basic-interactions', 'relayoutData')]) def display_selected_data(relayoutData): return json.dumps(relayoutData, indent=2)