Skip to content

Commit 381bd56

Browse files
authored
Login frontend (gradio-app#499)
1 parent 7a67fa0 commit 381bd56

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

demo/hello_login/run.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import gradio as gr
2+
3+
user_db = {"admin": "admin", "foo": "bar"}
4+
5+
def greet(name):
6+
return "Hello " + name + "!!"
7+
8+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
9+
if __name__ == "__main__":
10+
iface.launch(auth=lambda u, p: user_db.get(u) == p)

demo/hello_login/screenshot.png

16 KB
Loading

demo/hello_world/run.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import gradio as gr
22

3-
43
def greet(name):
54
return "Hello " + name + "!!"
65

7-
86
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
97
if __name__ == "__main__":
108
iface.launch()

demo/hello_world_2/run.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import gradio as gr
22

3-
43
def greet(name):
54
return "Hello " + name + "!"
65

7-
86
iface = gr.Interface(
97
fn=greet,
108
inputs=gr.inputs.Textbox(lines=2, placeholder="Name Here..."),

demo/hello_world_3/run.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import gradio as gr
22

3-
43
def greet(name, is_morning, temperature):
54
salutation = "Good morning" if is_morning else "Good evening"
65
greeting = "%s %s. It is %s degrees today" % (salutation, name, temperature)
76
celsius = (temperature - 32) * 5 / 9
87
return greeting, round(celsius, 2)
98

10-
119
iface = gr.Interface(
1210
fn=greet,
1311
inputs=["text", "checkbox", gr.inputs.Slider(0, 100)],

frontend/src/Login.svelte

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
export let root;
3+
</script>
4+
5+
<div class="login container mt-8">
6+
<form
7+
class="mx-auto p-4 bg-gray-50 shadow-md w-1/2"
8+
id="login"
9+
method="POST"
10+
action={root + "login"}
11+
>
12+
<h2 class="text-2xl font-semibold my-2">login</h2>
13+
14+
<label class="block uppercase mt-4" for="username">username</label>
15+
<input class="p-2 block" type="text" name="username" />
16+
<label class="block uppercase mt-4" for="password">password</label>
17+
<input class="p-2 block" type="password" name="password" />
18+
<input
19+
type="submit"
20+
class="block bg-yellow-500 hover:bg-yellow-400 dark:hover:bg-yellow-600 transition px-4 py-2 rounded text-white font-semibold cursor-pointer mt-4"
21+
/>
22+
</form>
23+
</div>

frontend/src/main.js

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
import App from './App.svelte';
2+
import Login from "./Login.svelte";
23
import { fn } from "./api";
34

45
window.launchGradio = (config, element_query) => {
56
let target = document.querySelector(element_query);
6-
let url = new URL(window.location.toString());
7-
if (config.theme !== null && config.theme.startsWith("dark")) {
8-
target.classList.add("dark");
9-
config.dark = true;
10-
if (config.theme === "dark") {
11-
config.theme = "default";
12-
} else {
13-
config.theme = config.theme.substring(5);
14-
}
15-
} else if (url.searchParams.get("__dark-theme") === "true") {
16-
config.dark = true;
17-
target.classList.add("dark");
18-
}
197
if (config.root === undefined) {
208
config.root = "BACKEND_URL";
219
}
22-
config.fn = fn.bind(null, config.root + "api/");
23-
const app = new App({
24-
target: target,
25-
props: config
26-
});
10+
if (config.detail === "Not authenticated") {
11+
new Login({
12+
target: target,
13+
props: config
14+
});
15+
} else {
16+
let url = new URL(window.location.toString());
17+
if (config.theme !== null && config.theme.startsWith("dark")) {
18+
target.classList.add("dark");
19+
config.dark = true;
20+
if (config.theme === "dark") {
21+
config.theme = "default";
22+
} else {
23+
config.theme = config.theme.substring(5);
24+
}
25+
} else if (url.searchParams.get("__dark-theme") === "true") {
26+
config.dark = true;
27+
target.classList.add("dark");
28+
}
29+
config.fn = fn.bind(null, config.root + "api/");
30+
new App({
31+
target: target,
32+
props: config
33+
});
34+
}
2735
}
2836

2937
async function get_config() {

0 commit comments

Comments
 (0)