[+] Inject markdown
This commit is contained in:
@@ -1,13 +1,7 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
# Constants (The instructors said that we can use global constants here:
|
# Constants (The instructors said that we can use global constants here:
|
||||||
# https://piazza.com/class/ksovzjrlsye72f?cid=1664
|
# https://piazza.com/class/ksovzjrlsye72f?cid=1664
|
||||||
# They should not end with "/"
|
# They should not end with "/"
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
DATA_DIR = './data'
|
DATA_DIR = './data'
|
||||||
TWEETS_DIR = f'{DATA_DIR}/twitter/user-tweets'
|
TWEETS_DIR = f'{DATA_DIR}/twitter/user-tweets'
|
||||||
USER_DIR = f'{DATA_DIR}/twitter/user'
|
USER_DIR = f'{DATA_DIR}/twitter/user'
|
||||||
REPORT_DIR = './report'
|
REPORT_DIR = './report'
|
||||||
# Sources directory. This may be different from the data directory if the running
|
|
||||||
SRC_DIR = str(Path(os.path.realpath(__file__)).parent)
|
|
||||||
|
|||||||
+13
-6
@@ -1,19 +1,23 @@
|
|||||||
import os.path
|
import os.path
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import markdown
|
from flask import Flask, send_from_directory
|
||||||
|
|
||||||
from constants import SRC_DIR, REPORT_DIR
|
from constants import REPORT_DIR
|
||||||
from utils import read
|
from utils import read
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
src_dir = Path(os.path.realpath(__file__)).parent
|
||||||
|
|
||||||
|
|
||||||
def generate_report() -> str:
|
def generate_report() -> str:
|
||||||
"""
|
"""
|
||||||
Do data visualization and generate a HTML report
|
Compile the report document and generate a markdown report
|
||||||
|
|
||||||
:return: HTML
|
:return: Markdown report
|
||||||
"""
|
"""
|
||||||
# Load markdown
|
# Load markdown
|
||||||
md = read(f'{SRC_DIR}/report_document.md').replace('\r\n', '\n').split('\n')
|
md = read(str(src_dir.joinpath('report_document.md'))).replace('\r\n', '\n').split('\n')
|
||||||
|
|
||||||
# Process @include statements
|
# Process @include statements
|
||||||
for i in range(len(md)):
|
for i in range(len(md)):
|
||||||
@@ -21,7 +25,10 @@ def generate_report() -> str:
|
|||||||
if line.startswith('@include'):
|
if line.startswith('@include'):
|
||||||
line = line[line.index('`') + 1:]
|
line = line[line.index('`') + 1:]
|
||||||
line = line[:line.index('`')]
|
line = line[:line.index('`')]
|
||||||
md[i] = read(line)
|
md[i] = read(REPORT_DIR + line)
|
||||||
|
|
||||||
|
return '\n'.join(md)
|
||||||
|
|
||||||
|
|
||||||
return markdown.markdown('\n'.join(md), extensions=['tables'])
|
return markdown.markdown('\n'.join(md), extensions=['tables'])
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
Test Image:
|
Test Image:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Test Include:
|
Test Include:
|
||||||
|
|
||||||
@include `{REPORT_DIR}/1-covid-tweet-frequency/500-pop.md`
|
@include `/1-covid-tweet-frequency/500-pop.md`
|
||||||
|
|||||||
@@ -9,9 +9,16 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.getElementById('content').innerHTML =
|
|
||||||
marked.parse('# Marked in the browser\n\nRendered by **marked**.');
|
// Python will inject the markdown code here.
|
||||||
|
markdown = `
|
||||||
|
{{markdown}}
|
||||||
|
`
|
||||||
|
document.getElementById('content').innerHTML =
|
||||||
|
marked.parse(markdown);
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user