simplify this jinja template ''' <!DOCTYPE html> <html lang="en"> <head> <meta…
- User
simplify this jinja template '''
('#CB_OPF_table').find('tr').each(function() { var table = $(this);
if (!($table.find('td').text().search(patt) >= 0)) { $table.not('.myHead').hide(); } if (($table.find('td').text().search(patt) >= 0)) { $(this).show(); }});
});
("#ResetButton").each(function() { (this).click(function(){ ("#SearchBarInput").val(''); location.reload(); ("td").css({"white-space": "nowrap"}); }) });
});
{%- for col_header in df.columns.tolist() %} {%- if col_header in ['Item Type','Location','SN','Manufacturer','ManuDate','DesignReg','PlantReg','DP','Vol','InspDate'] %}
{%- elif col_header in ['AMEID','FLOC','SAPID','NextExt', 'NextInt','Notes','Recommendation','MaintPlan','DrawingFile','InspectionAccess'] %}
{%- elif col_header in ['HZL'] %}
{%- elif col in ['AMEID', 'FLOC','SAPID', 'Notes','Recommendation','MaintPlan','InspectionAccess'] %} <td class=" {{col | lower }}">{{row[col]}}</td> {%- elif col in ['HZL'] %} <td class="hazardlevel">{{row[col]}}</td> {%- elif col in ['SN'] %} {%- if row['MDRFile'] | length > 3 %} {%- set file=row['MDRFile'].strip("[]' ") %} <td class="url"><a href="{{ '/'.join([root,index,file]) }}">{{ row[col] }}</a></td> {%- else %} <td class="url">{{ row[col] }}</a></td> {%- endif %} {%- elif col in ['DrawingFile'] %} {%- if row[col] | length > 3 and row[col] !='' %} <td class="{{col | lower }} url"> {% for file in row['DrawingFile'].split(",") |reverse %} {% if loop.first %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" >{{index}} GA</a>  {% if row['DrawingFile'].split(",") |length>1 %} <button class="more-button" onclick="showHide(this)"> <i class="fa fa-plus"></i> </button>   {% endif %} {% else %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" style="display:none">{{ file.strip("[]' ") }}</a>   {% endif %} {% endfor %} </td> {%- elif row[col]=='nan' %} <td class="{{col | lower }} "></td> {%- endif %} {%- elif col in ['InspDate'] %} {%- if row['InspReportFile'] | length > 3 and row[col] !='' %} <td class="url"> {% for file in row['InspReportFile'].split(",") |reverse %} {% if loop.first %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" >{{ row[col] }}</a>  {% if row['InspReportFile'].split(",") |length>1 %} <button class="more-button" onclick="showHide(this)"> <i class="fa fa-plus"></i> </button>   {% endif %} {% else %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" style="display:none">{{ file.strip("[]' ") |regex_replace }}</a>   {% endif %} {% endfor %} </td> {%- else %} {%- if row[col] =='' %} <td class="noinspect">not inspected</a></td> {%- else %} <td class="noinspect">{{row[col]}}</a></td> {%- endif %} {%- endif %} {%- elif col in ['DesignReg'] %} {%- if row['DesignRegFile'] | length > 3 %} {%- set file=row['DesignRegFile'].strip("[]' ") %} <td class="url"><a href="{{ '/'.join([root,index,file]) }}">{{ row[col] }}</a></td> {%- else %} <td class="url">{{ row[col] }}</a></td> {%- endif %} {%- elif col in ['PlantReg',] %} {%- if row['PlantRegFile'] | length > 3 %} {%- set file=row['PlantRegFile'].strip("[]' ") %} <td class="url"><a href="{{ '/'.join([root,index,file]) }}">{{ row[col] }}</a></td> {%- else %} <td class="url">{{ row[col] }}</a></td> {%- endif %} {%- elif col in ['ManuDate','NextExt', 'NextInt'] %} <td class="{{ col | lower }}">{{ row[col] }}</a></td> {%- endif %} - Assistant
- User
that's not what I want don't change the formatting, I want you to act like a senior coder to review and improve the code
- Assistant
I apologize for the misunderstanding. Here's an improved version of your Jinja template with some modifications to enhance the code:
- Moved the inline JavaScript to an external file named
scripts.js. - Added
deferattribute to script tags to ensure the page content is loaded before the scripts execute.
- Moved the inline JavaScript to an external file named
- User
can you also improve jinja template itself?
- Assistant
- User
improve below 2 elif loop to one elif: {%- elif col in ['DrawingFile'] %} {%- if row[col] | length > 3 and row[col] !='' %}
{% for file in row['DrawingFile'].split(",") |reverse %} {% if loop.first %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" >{{index}} GA  {% if row['DrawingFile'].split(",") |length>1 %}  
{% endif %} {% else %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" style="display:none">{{ file.strip("[]' ") }}   {% endif %} {% endfor %} {%- elif row[col]=='nan' %}
{%- endif %}{%- elif col in ['InspDate'] %} {%- if row['InspReportFile'] | length > 3 and row[col] !='' %} <td class="url"> {% for file in row['InspReportFile'].split(",") |reverse %} {% if loop.first %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" >{{ row[col] }}</a>  {% if row['InspReportFile'].split(",") |length>1 %} <button class="more-button" onclick="showHide(this)"> <i class="fa fa-plus"></i> </button>   {% endif %} {% else %} <a href="{{ '/'.join([root,index,file.strip("[]' ")]) }}" style="display:none">{{ file.strip("[]' ") |regex_replace }}</a>   {% endif %} {% endfor %} </td> {%- else %} {%- if row[col] =='' %} <td class="noinspect">not inspected</a></td> {%- else %} <td class="noinspect">{{row[col]}}</a></td> {%- endif %} {%- endif %} - Assistant
I've combined the two
elifloops into a single one and moved the conditional checks inside the loop.{%- elif col in ['DrawingFile', 'InspDate'] %} {%- set file_key = 'DrawingFile' if col == 'DrawingFile' else 'InspReportFile' %} {%- set content = index ~ ' GA' if col == 'DrawingFile' else row[col] %} {%- if row[file_key] | length > 3 and row[col] != '' %} <td class="{{ col | lower }} url"> {% for file in row[file_key].split(",") | reverse %} {% if loop.first %} <a href="{{ '/'.join([root, index, file.strip("[]' ")]) }}">{{ content }}</a>  {% if row[file_key].split(",") | length > 1 %} <button class="more-button" onclick="showHide(this)"> <i class="fa fa-plus"></i> </button>   {% endif %} {% else %} <a href="{{ '/'.join([root, index, file.strip("[]' ")]) }}" style="display:none">{{ file.strip("[]' ") | regex_replace }}</a>   {% endif %} {% endfor %} </td> {%- elif row[col] == 'nan' %} <td class="{{ col | lower }}"></td> {%- else %} <td class="noinspect">{{ content or 'not inspected' }}</td> {%- endif %}