File size: 2,192 Bytes
f3a39f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
import visual
import gen
import subprocess
import sys

def showpuzzle(encoding):
    p1 = visual.parse(encoding)
    return visual.fig(p1)

showpuzzint = gr.Interface(fn=showpuzzle, inputs=gr.Text(label="Puzzle encoding"), outputs=gr.Plot())


def genpuzzle(width,height,is_square,shuffle,seed):
    if is_square:
        height=width
    p = gen.genpuzzle(width,height,seed)
    if seed==0:
        seed=gen.seed1
    if not shuffle:
        return visual.fig(p),seed,gen.h(p),''
    p_ = gen.shuf(p)
    p2 = visual.join(p,p_)
    return visual.fig(p2),seed,gen.h(p),gen.h(p_)

genint = gr.Interface(fn=genpuzzle, inputs=[gr.Slider(label="Width",step=1,minimum=1,maximum=40,value=3),gr.Slider(label="Height",step=1,minimum=1,maximum=40,value=3),gr.Checkbox(label="Square",value=True),gr.Checkbox(label="Shuffle"),gr.Number(label="Seed",value=0)], outputs=[gr.Plot(label="Puzzle"),gr.Number(label="Seed"),gr.Text(label="Puzzle encoding"),gr.Text(label="Shuffled encoding")])

def shuffle(encoding,show):
    p = visual.parse(encoding)
    p_ = gen.shuf(p)
    if show:
        return visual.fig(p_),gen.h(p_)
    return None,gen.h(p_)

shufint = gr.Interface(fn=shuffle, inputs=[gr.Text(label="Puzzle encoding"),gr.Checkbox(label="Visualise")], outputs=[gr.Plot(label="Visualisation"),gr.Text(label="Shuffled encoding")])

def sendcommand(function,args):
    process = subprocess.run(["ghc", "Challenges.hs", "-e", f'{function} {args}' ], check=True, capture_output=True)
    return process.stdout.decode(sys.stdout.encoding).strip()

commandint = gr.Interface(fn=sendcommand, inputs=['text','text'], outputs='text', description="sends a function call to Challenges.hs")

def runtests():
    output = subprocess.run(["runghc", "../Tests.hs"],check=True,capture_output=True).stderr.decode(sys.stdout.encoding).strip()
    if output == "":
        return "All tests passed"
    return output
    
testint = gr.Interface(fn=runtests, inputs=None, outputs='text', description="runs automated tests from Tests.hs")

demo = gr.TabbedInterface([showpuzzint,genint,shufint,commandint,testint],["visualise","generate","shuffle","run","test"],title="comp2209 Tools")
demo.launch()