You can create an HTML file that includes two interactive plots and makes them accessible via tabs using Python. Here's a step-by-step guide to help you achieve this:
- Create the HTML layout: Use basic HTML and CSS to set up the tabbed interface.
- Embed your plot HTML strings: Include the interactive plot HTML strings in the appropriate tabs.
Here is a sample Python code to do this:
# Sample interactive plot HTML strings
plot1_html = """
<div>
<!-- Your first interactive plot HTML -->
<h2>Interactive Plot 1</h2>
<p>This is the content of interactive plot 1.</p>
<!-- Your interactive plot can go here, for example: -->
<iframe src="path_to_plot1.html" width="600" height="400"></iframe>
</div>
"""
plot2_html = """
<div>
<!-- Your second interactive plot HTML -->
<h2>Interactive Plot 2</h2>
<p>This is the content of interactive plot 2.</p>
<!-- Your interactive plot can go here, for example: -->
<iframe src="path_to_plot2.html" width="600" height="400"></iframe>
</div>
"""
# HTML code to create tabs
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tabbed Plot Viewer</title>
<style>
body {{
font-family: Arial, sans-serif;
}}
.tab {{
display: none;
}}
.tab-button {{
padding: 10px;
cursor: pointer;
border: 1px solid #ddd;
border-bottom: none;
background-color: #f1f1f1;
margin-right: 5px;
}}
.tab-button:hover {{
background-color: #e7e7e7;
}}
.tab-content {{
border: 1px solid #ddd;
padding: 10px;
}}
.active {{
display: block;
}}
</style>
</head>
<body>
<h1>Interactive Plots</h1>
<div>
<button class="tab-button" onclick="openTab(event, 'Plot1')">Plot 1</button>
<button class="tab-button" onclick="openTab(event, 'Plot2')">Plot 2</button>
</div>
<div id="Plot1" class="tab tab-content">
{plot1_html}
</div>
<div id="Plot2" class="tab tab-content">
{plot2_html}
</div>
<script>
function openTab(evt, tabName) {{
var i, tabcontent, tabbuttons;
// Hide all tab content
tabcontent = document.getElementsByClassName("tab");
for (i = 0; i < tabcontent.length; i++) {{
tabcontent[i].style.display = "none";
}}
// Remove the active class from all buttons
tabbuttons = document.getElementsByClassName("tab-button");
for (i = 0; i < tabbuttons.length; i++) {{
tabbuttons[i].className = tabbuttons[i].className.replace(" active", "");
}}
// Show the current tab and add an "active" class to the button that opened the tab
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}}
// Automatically open the first tab
document.getElementsByClassName("tab-button")[0].click();
</script>
</body>
</html>
"""
# Save the HTML content to a file
with open("interactive_plots.html", "w") as f:
f.write(html_content)
print("HTML file with interactive plots created successfully.")
Explanation:
- Tabs Creation: The HTML file has buttons to switch between the plots. Each plot resides in a div that is styled to be hidden or shown based on user actions.
- JavaScript Handling: A small JavaScript function (
openTab) is defined to control which plot (tab) is currently displayed.
- Embedding iFrames: You can replace the
iframe source with your actual interactive plot HTML codes or paths.
- Save to File: Finally, the constructed HTML content is written to a file, which can be opened in a web browser.
Note:
- Make sure you have the correct paths for your plots if they are in separate HTML files.
- This script assumes standalone HTML strings that include interactivity. If your plots are created with libraries (like Plotly, Bokeh, etc.), ensure you properly export the plots as HTML strings.