pull/1/head
韩天峰 3 years ago
parent 61823110ab
commit 6cf8366b61
  1. 2
      cases/test.py
  2. 45
      conv.php
  3. 4
      web/include/style.php
  4. 17
      web/public/convert.php
  5. 32
      web/public/index.php
  6. 4
      web/public/static/css/style.css
  7. 9789
      web/public/static/js/jquery-1.10.2.js

@ -16,10 +16,8 @@ def test(name, n, hello, **kwargs):
model2 = vLLMWrapper('Qwen/Qwen-72B-Chat', tensor_parallel_size=2) model2 = vLLMWrapper('Qwen/Qwen-72B-Chat', tensor_parallel_size=2)
def it(m): def it(m):
return "hello world" return "hello world"
s = it(model2) s = it(model2)
return model2 return model2

@ -1,17 +1,14 @@
<?php <?php
use PhpyTool\Tool;
if ($argc < 2) { if ($argc < 2) {
die("Usage: php conv.php [python-file]\n"); die("Usage: php conv.php [python-file]\n");
} }
require_once __DIR__ . '/src/Tool.php'; $py_script = __DIR__ . '/dump.py';
$result = shell_exec('python ' . $py_script . ' ' . $argv[1]);
$result = shell_exec('python dump.py ' . $argv[1]);
$json = json_decode($result); $json = json_decode($result);
if (empty($json)) {
die("error py code");
}
if ($json->_type != 'Module') { if ($json->_type != 'Module') {
echo "invalid python module\n"; echo "invalid python module\n";
} }
@ -173,9 +170,39 @@ class Translator
} }
} }
static function valueToRepr($v, $python = false)
{
if (is_string($v)) {
$v = str_replace(
["\\", "\n", "\r", "\t", "\v", "\x00", "\""],
["\\\\", "\\n", "\\r", "\\t", "\\v", "\\x00", "\\\""],
$v);
return "\"$v\"";
} elseif ($v === []) {
return '[]';
} elseif (is_numeric($v)) {
if ($python) {
if (is_infinite($v)) {
return "float('inf')";
} elseif (is_nan($v)) {
return "float('nan')";
}
}
return strval($v);
} elseif (is_bool($v)) {
return $python ? ($v ? 'True' : 'False') : ($v ? 'true' : 'false');
} elseif (is_null($v)) {
return $python ? 'None' : 'null';
} elseif (is_array($v)) {
return var_export($v, true);
} else {
return $python ? 'None' : 'null';
}
}
function parseConstant($value): string function parseConstant($value): string
{ {
return Tool::valueToRepr($value->value); return self::valueToRepr($value->value);
} }
function parseTuple($tuple) function parseTuple($tuple)

@ -1,5 +1,5 @@
<select id="selectStyle"> <select id="selectStyle">
<option>a11y-dark.min.css</option> <option selected="selected">a11y-dark.min.css</option>
<option>a11y-light.min.css</option> <option>a11y-light.min.css</option>
<option>agate.min.css</option> <option>agate.min.css</option>
<option>an-old-hope.min.css</option> <option>an-old-hope.min.css</option>
@ -68,7 +68,7 @@
<option>tomorrow-night-blue.min.css</option> <option>tomorrow-night-blue.min.css</option>
<option>tomorrow-night-bright.min.css</option> <option>tomorrow-night-bright.min.css</option>
<option>vs.min.css</option> <option>vs.min.css</option>
<option selected>vs2015.min.css</option> <option>vs2015.min.css</option>
<option>xcode.min.css</option> <option>xcode.min.css</option>
<option>xt256.min.css</option> <option>xt256.min.css</option>
<option>base16/3024.min.css</option> <option>base16/3024.min.css</option>

@ -0,0 +1,17 @@
<?php
if (empty($_POST['code'])) {
die('require code');
}
$file = tempnam('/tmp', 'py2php');
file_put_contents($file, $_POST['code']);
$conv = dirname(__DIR__, 2) . '/conv.php';
$cmd = 'php ' . $conv . ' ' . $file;
$out = shell_exec($cmd);
echo json_encode([
'data' => ['code' => $out, 'cmd' => $cmd]
]);
unlink($file);

@ -5,9 +5,10 @@
<head> <head>
<title>Convert Python code to PHP</title> <title>Convert Python code to PHP</title>
<link href="/static/css/style.css" rel="stylesheet"/> <link href="/static/css/style.css" rel="stylesheet"/>
<link id='theme1' href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/vs2015.min.css" <link id='theme1' href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/a11y-dark.min.css"
rel="stylesheet"/> rel="stylesheet"/>
<script src="/static/js/codeEditorShortcutKeys.js" type="text/javascript"></script> <script src="/static/js/codeEditorShortcutKeys.js" type="text/javascript"></script>
<script src="/static/js/jquery-1.10.2.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js" <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"
type="text/javascript"></script> type="text/javascript"></script>
</head> </head>
@ -40,7 +41,8 @@ define('ROOT_PATH', dirname(__DIR__));
</div> </div>
<div style="margin-top: 8px"> <div style="margin-top: 8px">
<button onclick="" style="height: 32px; width: 60px">转换</button> <button id="btn-convert" style="height: 32px; width: 60px">转换</button>
<button id="btn-reset" style="height: 32px; width: 60px">重置</button>
</div> </div>
<h2>快捷键</h2> <h2>快捷键</h2>
@ -65,6 +67,26 @@ define('ROOT_PATH', dirname(__DIR__));
const codeBlock = document.getElementById("codeBlock"); const codeBlock = document.getElementById("codeBlock");
const lineNumbers = document.getElementById('lineNumbers'); const lineNumbers = document.getElementById('lineNumbers');
$('#btn-convert').click(function () {
const code = textarea1.value
$.post("/convert.php", {code: code}, function (result) {
const json = JSON.parse(result)
textarea1.value = json.data.code
$('#btn-convert').hide()
$('#selectLanguage').val('language-php')
updateLang()
updateCode()
});
})
$('#btn-reset').click(function () {
textarea1.value = ''
$('#btn-convert').show()
$('#selectLanguage').val('language-python')
updateLang()
updateCode()
})
function updateLineNumbers() { function updateLineNumbers() {
let lineCount = textarea1.value.split('\n').length; let lineCount = textarea1.value.split('\n').length;
let lines = ''; let lines = '';
@ -76,7 +98,6 @@ define('ROOT_PATH', dirname(__DIR__));
// copy code from textarea to code block // copy code from textarea to code block
function updateCode() { function updateCode() {
let content = textarea1.value; let content = textarea1.value;
// encode the special characters // encode the special characters
@ -92,6 +113,11 @@ define('ROOT_PATH', dirname(__DIR__));
highlightJS(); highlightJS();
} }
function updateLang() {
document.getElementById("codeBlock").className = $("#selectLanguage").val()
highlightJS()
}
// syntax highlight // syntax highlight
function highlightJS() { function highlightJS() {
document.querySelectorAll('pre code').forEach((el) => { document.querySelectorAll('pre code').forEach((el) => {

@ -1,5 +1,9 @@
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap');
select {
height: 24px;
}
#preCode code, #preCode code,
textarea, textarea,
#lineNumbers, #lineNumbers,

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save