def json2html(value: str) -> str: """ Converts any JSON formatted string to an HTML table. In case a conversion can not be made, because of an incorrect format or empty value, an empty string will be returned instead. Result of this needs to be inside a "safe" template filter to be rendered properly. :param value: Correctly formated JSON string. Or nothing, both are fine. :return: HTML table string. """ build_direction = "LEFT_TO_RIGHT" table_attributes = { "class": "table table-striped table-bordered importDetails" } try: json_obj = json.loads(value) except ValueError: return '' if type(json_obj) is list: html = "\n".join([ convert(json, build_direction=build_direction, table_attributes=table_attributes) for json in json_obj ]) return html elif type(json_obj) is dict: html = convert(json_obj, build_direction=build_direction, table_attributes=table_attributes) return html
def json2html(value: str)-> str: """ Converts any JSON formatted string to an HTML table. In case a conversion can not be made, because of an incorrect format or empty value, an empty string will be returned instead. Result of this needs to be inside a "safe" template filter to be rendered properly. :param value: Correctly formated JSON string. Or nothing, both are fine. :return: HTML table string. """ build_direction = "LEFT_TO_RIGHT" table_attributes = {"class": "table table-striped table-bordered importDetails"} try: json_obj = json.loads(value) except ValueError: return '' if type(json_obj) is list: html = "\n".join([convert(json, build_direction=build_direction, table_attributes=table_attributes) for json in json_obj]) return html elif type(json_obj) is dict: html = convert(json_obj, build_direction=build_direction, table_attributes=table_attributes) return html
def htmlShowInfo(cadiInfo): import json2table html = "<ul>" for cadiLine in cadiInfo: html += "<li>" + json2table.convert(cadiLine) + "</li>" html += "</ul>" return html
def json_comparer(query_result, expected_result): result_list = [x for x in expected_result if x in query_result] lista = [] for x in expected_result: diz = {} if x in result_list: diz['color'] = 'Green' else: diz['color'] = 'Red' build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:100%"} html = convert(x, build_direction=build_direction, table_attributes=table_attributes) diz['content'] = html lista.append(diz) if len(result_list) == len(expected_result): # 10 su 10 (li trova tutti) # TABELLA VERDE: per ogni elemento di expected_result return True, None, lista else: var = len(expected_result) - len(result_list) # 0 su 10 (non ne trova nessuno) if var == len(expected_result): return False, None, lista # TABELLA ROSSA: per ogni elemento di expected_result else: check_list = [m for m in expected_result if m not in result_list] # da 1 a 9 (ne trova qualcuno) #tabella colorata VERDE/ROSSA return True, str(len(check_list)), lista
def convert_to_html(data): with open('output.html', 'a') as f4: build_direction = "TOP_TO_BOTTOM" table_attributes = {"class": "myTable"} f4.write(convert(data, build_direction=build_direction, table_attributes=table_attributes)) f4.write('<br><br>')
def convert_json(json): build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "border:1px solid grey"} html = convert(json, build_direction=build_direction, table_attributes=table_attributes) return html
def getAdmin(): form = Admin() form.token.data = getRandomString( int(getenv('REGISTER_DEFAULT_TOKEN_LENGTH'))) if form.validate_on_submit(): data = { "token": form.token.data, "active": "1", "domain": form.domain.data, "local_part": form.username.data, "quota": str(form.quota.data), "force_pw_update": "0", "tls_enforce_in": int(form.tlsIn.data), "tls_enforce_out": int(form.tlsOut.data) } db_invitations.set(form.username.data, json.dumps(data)) # redirect to admin so you can reload the page without the warning "You will post data again" return redirect("/admin") currentUsers = sess.get(getUrl('/get/mailbox/all')).json() output = { 'activeUsers': [x['username'] for x in currentUsers], 'invitations': getInvitationData(db_invitations), 'expired': getInvitationData(db_expiredInvitations) } return render_template('index.html', url=getenv('MAILCOW_HOST'), form=form, table=convert(output), admin=True)
def make_html(d: Dict) -> str: """ Convert the given dictionary into an HTML table (string) with two columns: keys of dictionary, values of dictionary. """ return j2t.convert( d, table_attributes={"class": "table table-condensed table-hover"})
def get_teams(): json_object = response_data_teams build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:50%"} html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes) return html
def task_display_data(self): """ Display with dash. """ self.config.compact() # This line makes a cannot find reference warning and i do not know why and how i can fix it self.configText = html.Div([ ddsih.DangerouslySetInnerHTML(json2table.convert(self.config.data)) ])
def makeTable(environ, start_response): r = requests.get(url) stateAll = r.json() state = OrderedDict() state['SOC'] = str(stateAll['USOC']) + '%' state['Production'] = str(stateAll['Production_W']) + 'W' state['Consumption'] = str(stateAll['Consumption_W']) + 'W' state['Battery'] = str(stateAll['Pac_total_W']) + 'W' state['GridFeedIn'] = str(stateAll['GridFeedIn_W']) + 'W' state['Timestamp'] = stateAll['Timestamp'] build_direction = "LEFT_TO_RIGHT" state = json2table.convert(state, build_direction=build_direction) start_response('200 OK', [('Content-Type', 'text/html')]) if stateAll['GridFeedIn_W'] < -200: color = b'Red' elif stateAll['Pac_total_W'] > 50: color = b'Orange' elif stateAll['Pac_total_W'] < -50: color = b'green' elif stateAll['GridFeedIn_W'] > 200: color = b'DarkGreen' else: color = b'SkyBlue' return [ b""" <html> <head> <style> table { border-collapse: collapse; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) {background-color: #f2f2f2;} </style> </head> <body bgcolor="%(bgcolor)s"> %(table)s </body> </html>""" % { b"table": state.encode('utf-8'), b"bgcolor": color } ]
def Mail(emails, cc, msg): sub = status + ": " + msg['主题'] build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:100%", "border": "1"} #table_attributes = {"style": "width:100%;border:1px solid #000;", "border": "1"} html = convert(msg, build_direction=build_direction, table_attributes=table_attributes) pre = "详情请登录 <a href=\"" + dashhome + "\">" + dashhome + "</a><br/><hr>" suffix = "<hr><br><br><hr><b>智能云平台 运维组</b><br>主页: <a href=\"" + home + "\">" + home + "<br><hr><br>" html = pre + html + suffix send_mail(emails, cc, sub, html, 1)
def get_players(search): url_players = f"https://www.balldontlie.io/api/v1/players/?search={search}" r_players = requests.get(url_players, params=search) response_data_players = r_players.json() json_object = response_data_players build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:50%"} html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes) return html
def get_stats(player_id): url_stats = f"https://www.balldontlie.io/api/v1/season_averages?season=2019&player_ids[]={player_id}" r_stats = requests.get(url_stats, params=player_id) response_data_stats = r_stats.json() json_object = response_data_stats build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:10%"} html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes) return html
def datamart(): test1 = mongo.db.clustered.find({"Cluster Label":"AUTO"}, {'_id':0, 'Account Number':0, 'Good Cluster': 0, 'Cluster':0, 'Representation':0, 'Cluster Description':0, 'Location City':0}) #mongo returns bson test2 = dumps(test1) #converst bson tojson data = json.loads(test2) build_direction = "TOP_TO_BOTTOM" html = "" table_attributes = {"style" : "width:100%", "class" : "table table-striped"} for obj in range(len(data)): json_object = data[obj] html = html+convert(json_object, build_direction=build_direction, table_attributes=table_attributes) return render_template('index.html', html=html)
def showjson(): SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) json_url = os.path.join(SITE_ROOT, "static/json", "train_1.json") # data = json.load(open(json_url)) infoFromJson = json.load(open(json_url)) build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:100%"} table_html = json2table.convert(infoFromJson, build_direction=build_direction, table_attributes=table_attributes) return table_html
def dispatch_request(self): '''LOF in name or etfFeeders not --此时可以套利或者降成本操作''' tz_china = timezone('Asia/Shanghai') tz_utc = timezone('UTC') utc = datetime.utcnow().replace(tzinfo=tz_utc) _time = utc.astimezone(tz_china) time_china = datetime.time(_time) date_china = datetime.date(_time) #exclude taoli.json with none etffeeders _json = json.load(open('taoli.json')) #存在关联基金,可以近似套利 # _json['records'] = [x for x in _json['records'] \ # if len(x['关联基金']) > 10 or 'LOF' or 'QDII-ETF' in x['name']] #TODO: QDII included in LOF? # 折溢价达到标准,并且申购赎回对应开放了 _json['records'] = [x for x in _json['records'] \ if (float(x['navPriceRatioFcst'].replace('%',''))>=5 and (x['application'] == "1" or len(x['关联基金']) > 10)) or \ (float(x['navPriceRatioFcst'].replace('%',''))<=-3 and (x['redemption'] == "1" or len(x['关联基金']) > 10))] if _json['records']: table_attributes = {"border": 1} mail_html = convert(_json, table_attributes=table_attributes) subscriptions = Subscription.query.filter(or_(Subscription.last_send == None, Subscription.last_send != date_china)). \ filter(Subscription.time <= time_china).all() if subscriptions: app.logger.info( 'invest::preparing to send mail to {} subscriptions'. format(len(subscriptions))) for sub in subscriptions: addr = sub.email subject = '折溢价基金套利提醒{}'.format(date_china) recipients = [addr] sender = '*****@*****.**' msg = Message(subject, recipients=recipients) msg.html = "<p>请<a href={host}taoli/>点击这里</a>查看详情</p><hr>{html}".format( host=request.host_url, html=mail_html) mail.send(msg) app.logger.info('invest::sent mail to {}'.format(addr)) #更新last_send date, 1 mail/day sub.last_send = date_china db.session.commit() return 'mails sent to {} subscriptions'.format( len(subscriptions)) else: return {'code': 1, 'message': 'no subscription found'} else: return {'code': 2, 'message': 'nothing to notify'}
def accuracy(): rows1, rows2, rows3, rows4, rows5 = generate_accuracy_response() build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:100%"} data['table'] = convert([rows1, rows2, rows3, rows4, rows5], build_direction=build_direction, table_attributes=table_attributes) data['content'] = "Generated by ® Measurement Tool for Hackathon" data['fields'] = "" data['message'] = "Showing accuracy" data['header'] = "Requirements Tool" return render_template('templates/base.html', data)
def GETNOTEU(): #get requestas - gaunam JSON formatu duomenis r2 = requests.get(url = URL2, params = PARAMS2) infoFromJson2 = json.loads(r2.text) build_direction = "LEFT_TO_RIGHT" #spalva raudona nes ne Europa table_attributes = {"style": "width:100%, bgcolor:red"} #kiekviena eilute perrasom i txt faila for item in infoFromJson2: f.write(json2table.convert(item, build_direction=build_direction, table_attributes=table_attributes)+"\n")
def run_prediction(hash_train, hash_test): train_file = hash_train + ".csv" test_file = hash_test + ".csv" # Make predictions df_train = pd.read_csv(os.path.join('app/', 'uploads/', train_file)) df_test = pd.read_csv(os.path.join('app/', 'uploads/', test_file)) X, y = utils.X_y_split(X_train=df_train, X_test=df_test) model = fa.All() model.fit(X, y) # Append prediction column to test set predictions = model.predict(df_test) df_test['prediction'] = predictions # Save output file in /downloads folder df_test.to_csv("app/downloads/" + hash_train + "_" + hash_test + ".csv", index=False) # Add model.display_score to JSON and round values model.all_metrics = {k: round(v, 3) for k, v in model.all_metrics.items()} model.all_metrics['Overall score'] = model.display_score # Build HTML table build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:30%"} display_score = convert(model.all_metrics, build_direction=build_direction, table_attributes=table_attributes) # if df_train.shape[1]==(df_test.shape[1]-1): # temp=hash_test # hash_test=hash_train # hash_train=temp # temp_df=df_test # df_test=df_train # df_train=temp_df # # TESTING_DATA[hash_test]=df_test # TRAINING_DATA[hash_train]=df_train # #print("hash_train2",hash_train) # #print("hash_test2",hash_test) # #print("df_train2",df_train) # #print("df_test2",df_test) # flash("Uploaded files all training") # return redirect('home.html') # return jsonify({"hash":hash}) # return redirect(request.url) # return redirect(url_for('dataset')) return (jsonify({ "hashid": hash_train + "_" + hash_test, "performance": display_score }))
def to_table(self, data): # Work around bugs in json2table: if not isinstance(data, dict): datatype = type(data) if isinstance(data, list) and len(data) > 0: datatype = type(data[0]) temp = {} temp[datatype.__name__] = data data = temp # This is an ugly hack to expand the objects: data = self.parse(self.build(data)) # Finally convert json data to a table: return convert(data, build_direction="TOP_TO_BOTTOM", table_attributes={"class" : "table"})
def Http_Mail(emails, cc, msg, filelist): sub = status + ": " + msg['主题'] newmsg = copy.deepcopy(msg) linkimg = newmsg['关联图'] itemvalue = {"Data": newmsg['数据']} newmsg['状态'] += " " + newmsg['监控图表'] del newmsg['监控图表'] del newmsg['关联图'] del newmsg['数据'] pre = "<hr>" #pre = "详情请登录 <a href=\"" + dashhome + "\">" + dashhome + "</a><br/><hr>" if status != "OK": ackurl = genAckLink(newmsg) if ackurl != "": pre = '<p><a href="' + ackurl + '">' + ackinfo + '</a></p>' + pre try: del newmsg['eventid'] except: pass html = convert(newmsg, build_direction=build_direction, table_attributes=table_attributes) html_data = convert(itemvalue, build_direction=build_direction, table_attributes=table_attributes) if attachimg and show_linkimg: html = '<table><tr><td style="width:60%">' + html + '</td><td style="width:40%">' + linkimg + "</td></tr></table>" if not attachimg: filelist = [] html = "<h3>基本信息</h3>" + html + "<br><h3>监控项数据</h3>" + html_data link = "<p><h3>报警对象影响范围图</h3></p><p>" + linkimg + "</p>" suffix = "<br><p><hr><b>" + team + "</b><br>主页: <a href=\"" + home + "\">" + home + "<br><hr><br></p>" #suffix = "<hr><br>" + link + "<br><hr><b>" + team + "</b><br>主页: <a href=\"" + home + "\">" + home + "<br><hr><br>" html = pre + html + suffix html = re.sub('</?(ul|li)>', '', html) http_send_attachmail(emails, cc, sub, html, filelist)
def to_table(self, data): # Work around bugs in json2table: if not isinstance(data, dict): datatype = type(data) if isinstance(data, list) and len(data) > 0: datatype = type(data[0]) temp = {} temp[datatype.__name__] = data data = temp # This is an ugly hack to expand the objects: data = self.parse(self.build(data)) # Finally convert json data to a table: return convert(data, build_direction="TOP_TO_BOTTOM", table_attributes={"class": "table"})
def _repr_html_(self): return "".join( json2table.convert(json_obj) for json_obj in [{ "Task name: ": self._nbconfig["task_name"] }, { "Experiment info: ": self._nbconfig["experiment_info"] }, { "Datasets: ": list(self._nbconfig["tmp_data"].keys()) }, { "Created on ": self._creation_timestamp.strftime("%Y-%m-%d %H:%M") }, { "Contents: ": self._nbconfig }])
def get_probe_html_table(self): task = Task.objects.filter(media=self, type=TaskType.objects.get(name='PROBE'), state=Task.SUCCESS).last() output = TaskOutput.objects.filter(task=task, type=TaskOutput.JSON).last() if output is not None: return json2table.convert( json.load(open(output.file_path)), build_direction="LEFT_TO_RIGHT", table_attributes={ "class": "table table-bordered table-hover table-condensed" }) else: return None
def createTable(xmlinput): jsonstring = ( json.dumps(xmltodict.parse(xmlinput)) ) # this create an jsonstring-we can directly use jsonloads operation jsonobject = json.loads(jsonstring) #set table props build_direction = "LEFT_TO_RIGHT" table_attributes = { "style": "width:100%", "border": 1, "border-collapse": "collapse" } tableout = json2table.convert(jsonobject, build_direction=build_direction, table_attributes=table_attributes) return tableout
def dump_html_report(errors_dict={}, html_report_location=None): if not html_report_location: html_report_location = os.path.join(os.getenv('HOME'), 'valgrind_html_report.html') global html for log_file_name in errors_dict.keys(): for error_type in [ 'memory_leaks', 'syscall_ioctls', 'cond_jump_errors' ]: while ('' in errors_dict.get(log_file_name).get(error_type)): errors_dict.get(log_file_name).get(error_type).remove('') html += json2table.convert(errors_dict, build_direction=HTML_BUILD_DIRECTION) html = __get_html_string_formattted(html) with open(html_report_location, 'w+') as reportFile: reportFile.write(html)
def table_filling(daysandhoursform): if request.method == 'POST': try: #insert username and his choosen dates to the db mongo.db.meetings.update_one( {'_id': ObjectId(session['meeting_id'])}, { "$set": { 'users.' + session['current_user_id']: { 'name': daysandhoursform.name.data, 'selected_dates': json.loads( daysandhoursform.selecteddaysandhours.data) } } }) return 'Updated' except Exception as e: flash("Something wrong") print(e) else: #extract dates for this meeting from db and create HTML table from it dates_json = mongo.db.meetings.find_one( {'_id': ObjectId(session['meeting_id'])}) available_dates_json = dates_json['available_dates'] duration = dates_json['duration'] print(available_dates_json) print(duration) for day in available_dates_json['days']: for hour in day['hours']: day['hours'][hour] = day['hours'][hour] + ' - ' + ':'.join([ str(elem) for elem in (str( datetime.datetime.strptime(day['hours'][hour], '%H:%M') + datetime.timedelta(hours=int(duration.split(':')[0]), minutes=int(duration.split(':')[1])) ).split()[1].split(':')[:-1]) ]) # oneliner to make interval from start and duration build_direction = "TOP_TO_BOTTOM" table_attributes = {"class": "result__table"} #table = json2table.convert(infoFromJson,build_direction=build_direction,table_attributes=table_attributes)) table = json2table.convert(available_dates_json, build_direction=build_direction, table_attributes=table_attributes) return table return False
def view(): accounts_dict = {x.id: x.balance for x in database.get_accounts()} return """ <form style:"display:flex" method="get" action="/randomize"> <button type="submit">Ranzomize</button> </form> <form style:"display:flex" method="get" action="/delete"> <button type="submit">Delete</button> </form> <form style:"display:flex" method="get" action="/create"> <button type="submit">Create</button> </form> """ \ + convert(accounts_dict, table_attributes=table_attributes)
def read(): try: client = pymongo.MongoClient('localhost', 27017) #Db connect db = client.dbx empCol = db.formx.find() for emp in empCol: temp = emp build_direction = "LEFT_TO_RIGHT" table_attributes = {"style": "width:100%"} html = convert(emp, build_direction=build_direction, table_attributes=table_attributes) file = open('testfile.html', 'w') file.write(html) file.close() pdfkit.from_file('testfile.html', 'application.pdf') except Exception, e: print str(e)
def generatetable(): # make sure data is sent to data xmldata = '{"Message": "Hi there, we didnt get your message","ResponseStatus": "Waiting"}' xmldata = request.data #get the xml content from the request #print(xmldata) jsonstring = ( json.dumps(xmltodict.parse(xmldata)) ) # this create an jsonstring-we can directly use jsonloads operation jsonobject = json.loads(jsonstring) build_direction = "LEFT_TO_RIGHT" table_attributes = { "style": "width:100%", "border": 1, "border-collapse": "collapse" } tableout = json2table.convert(jsonobject, build_direction=build_direction, table_attributes=table_attributes) return tableout
def do_GET(self): try: self.send_response(200) self.send_header('Content-Type', 'application/json') fh = open("mouse_log.txt", "r") json_data = {} json_data["mouseActions"] = [] while True: line = fh.readline() if ("" == line): print("file finished") break lis = line.split(" ", 2) json_data["mouseActions"].append({ 'date': lis[0], 'time': lis[1], 'action': lis[2] }) # json_str=json.loads(json_data) except: self.send_response(404) build_direction = "LEFT_TO_RIGHT" table_attributes = { "style": "width:100%", "class": "table table-striped" } html = convert(json_data, build_direction=build_direction, table_attributes=table_attributes) table = json2html.convert(json=json_data) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes(table, "utf-8"))