Skip to content

Commit

Permalink
Merge pull request #326 from joerick/html-features
Browse files Browse the repository at this point in the history
HTML renderer upgrade
  • Loading branch information
joerick authored Oct 11, 2024
2 parents 4b37f8c + a51f692 commit 92464eb
Show file tree
Hide file tree
Showing 86 changed files with 4,462 additions and 1,335 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ jobs:
CIBW_ARCHS: ${{matrix.archs}}
CIBW_ARCHS_MACOS: auto universal2

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
path: ./wheelhouse/*.whl

build_sdist:
Expand All @@ -68,8 +69,9 @@ jobs:
- name: Build sdist
run: python setup.py sdist

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: dist/*.tar.gz

upload_pypi:
Expand All @@ -85,9 +87,11 @@ jobs:
attestations: write

steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: artifact
# unpacks all CIBW artifacts into dist/
pattern: cibw-*
path: dist
merge-multiple: true

- uses: pypa/gh-action-pypi-publish@release/v1
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
exclude: "\\.(json)$"
exclude: "\\.(json)$|docs/_static/preview"
args:
- --ignore-words-list=vas

Expand Down
6 changes: 6 additions & 0 deletions bin/build_js_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
JS_BUNDLE = "pyinstrument/renderers/html_resources/app.js"
CSS_BUNDLE = "pyinstrument/renderers/html_resources/app.css"

DOCS_PREVIEW_DIR = "docs/_static/preview"

if __name__ == "__main__":
# chdir to root of repo
os.chdir(os.path.dirname(__file__))
Expand Down Expand Up @@ -45,3 +47,7 @@

shutil.copyfile(HTML_RENDERER_DIR + "/dist/pyinstrument-html.iife.js", JS_BUNDLE)
shutil.copyfile(HTML_RENDERER_DIR + "/dist/style.css", CSS_BUNDLE)

subprocess.check_call("npm run build -- --mode preview", cwd=HTML_RENDERER_DIR, shell=True)
shutil.rmtree(DOCS_PREVIEW_DIR, ignore_errors=True)
shutil.copytree(HTML_RENDERER_DIR + "/dist", DOCS_PREVIEW_DIR)
19 changes: 19 additions & 0 deletions bin/create_demo_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3

import os
import subprocess
import sys
from glob import glob
from pathlib import Path

ROOT_DIR = Path(__file__).parent.parent


def main():
os.chdir(ROOT_DIR)
for script in glob(str("examples/demo_scripts/*.py")):
subprocess.run([sys.executable, "bin/create_sample_json.py", script], check=True)


if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions bin/create_sample_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
import argparse
import subprocess
import sys
from pathlib import Path

ROOT_DIR = Path(__file__).parent.parent
OUTPUT_DIR = ROOT_DIR / "html_renderer" / "demo-data"


def main():
parser = argparse.ArgumentParser()
parser.add_argument("SCRIPT", help="The script to run to produce the sample", type=Path)
args = parser.parse_args()
script_file: Path = args.SCRIPT
output_file = (OUTPUT_DIR / script_file.with_suffix("").name).with_suffix(".json")

result = subprocess.run(
[
"pyinstrument",
"-o",
str(output_file),
"-r",
"pyinstrument.renderers.html.JSONForHTMLRenderer",
script_file,
]
)

if result.returncode != 0:
return result.returncode

print(f"Sample JSON written to {output_file}")


if __name__ == "__main__":
sys.exit(main())

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions docs/_static/preview/assets/index-DhaBtkCM.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/_static/preview/assets/index-paBu1EOJ.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/_static/preview/assets/sympy_calculation-B9Pn_4RL.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions docs/_static/preview/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pyinstrument Demo</title>
<script type="module" crossorigin src="./assets/index-DhaBtkCM.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-paBu1EOJ.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
Binary file modified docs/img/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():
action="store",
type="int",
help="number of template render calls to make",
default=100,
default=200,
)
options, _ = parser.parse_args()

Expand All @@ -38,7 +38,11 @@ def main():
)
django.setup()

for _ in range(0, options.iterations):
render_templates(options.iterations)


def render_templates(iterations: int):
for _ in range(0, iterations):
django.template.loader.render_to_string("template.html")


Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions html_renderer/demo-data/django_template_render.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions html_renderer/demo-data/sympy_calculation.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions html_renderer/demo-data/wikipedia_article_word_count.json

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions html_renderer/demo-src/DemoApp.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<script lang="ts">
import pyinstrumentHTMLRenderer from "../src/main";
const fileURLs = import.meta.glob("../demo-data/*.json", {import: "default"})
const files = Object.entries(fileURLs).map(([srcURL, promiseFn]) => {
const filename = srcURL.split("/").pop()!;
const stem = filename.split(".").slice(0, -1).join(".");
return { name: stem, promiseFn };
});
let file = files[0];
let data: any = null;
let error: Error | null = null;
let loading = false;
$: {
loading = true;
error = null;
data = null;
file.promiseFn()
.then((json) => {
data = json;
error = null;
})
.catch((e) => {
error = e;
})
.finally(() => {
loading = false;
});
}
let appComponent: ReturnType<
(typeof pyinstrumentHTMLRenderer)["render"]
> | null = null;
let resultElement: HTMLElement | undefined;
$: if (resultElement && data) {
if (appComponent) {
appComponent.$destroy();
}
appComponent = pyinstrumentHTMLRenderer.render(resultElement, data);
}
</script>

<div class="demo-app">
<div class="header">
<div class="left"></div>
<div class="right">
Choose a demo profile:
<select bind:value={file}>
{#each files as optionFile}
<option value={optionFile}>{optionFile.name}</option>
{/each}
</select>
</div>
</div>
<div class="body">
{#if loading}
<div>Loading...</div>
{:else if error}
<div>Error loading file: {error.message}</div>
{/if}

<div class="result-element" style={!data ? 'display: none' : ''} bind:this={resultElement}></div>
</div>
</div>

<style lang="scss">
.demo-app {
background-color: #111;
color: white;
font-size: 11px;
font-family:
system-ui,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
"Helvetica Neue",
sans-serif;
display: flex;
flex-direction: column;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.header {
// background: #292f32;
// font-size: 14px;
padding: 5px 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.body {
position: relative;
flex: 1;
}
select {
font: inherit;
}
</style>
5 changes: 5 additions & 0 deletions html_renderer/demo-src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DemoApp from "./DemoApp.svelte";

new DemoApp({
target: document.body,
});
16 changes: 2 additions & 14 deletions html_renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pyinstrument</title>
<title>Pyinstrument Demo</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script type="module">
import pyinstrumentHTMLRenderer from "/src/main.ts";

fetch('/sample.json')
.then(response => response.json())
.then(data => {
pyinstrumentHTMLRenderer.render(
document.getElementById('app'),
data,
)
});
</script>
<script type="module" src="demo-src/main.ts"></script>
</body>
</html>
Loading

0 comments on commit 92464eb

Please sign in to comment.