

Using Daily’s video chat API with Anvil
source link: https://dev.to/britnellryan/using-daily-s-video-chat-api-with-anvil-42el
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adding video calls to your app with a few lines of code
Daily's API lets you add real time video calls to any app with just a few lines of code. Anvil makes it easy to build web apps entirely in Python -- no Javascript required. In this post, I'm going to show you how to use them together and show you how to get started with Daily's API.
We're going to build an app that lets your users join a video call. You could integrate it with anything -- communication for web based games, video-based collaboration software, or live customer support. (Click here to see some examples of apps you can build with Anvil.)
Daily call being startedWait -- isn't Daily a JavaScript API? That's right, but with Anvil you can import Javascript libraries into your Python front end. How? Read on...
Create a form to join video calls
Creating an app
Creating web apps with Anvil is simple. We'll create one to get started.
Log in to Anvil and click 'New Blank App'. Choose the Material Design theme.
Location of the Create App buttonCreating a UI
We need a form with a text box to enter the name of our meeting room and buttons to start and stop the call. Our finished form will look something like this:
Finished call formWe construct the form by dragging-and-dropping components. Let's start by dropping a Card into our form. Then drag a TextBox into the Card and, in the properties panel on the right, change the name of the component to room_name_textbox
.
Underneath the TextBox, drag and drop two Buttons. Change the first Button's name to start_call_button
and its text to Start call
. Change the second Button's name to end_call_button
and its text to End call
.
That's it! Our user interface is finished.
Using the Daily API
Creating a Daily video call room
We need to create a video call room our Anvil app users can join.
Let's do this by going to https://dashboard.daily.co/rooms, clicking Create room
and giving our room a name.
For more information on creating rooms, Daily has a useful guide here.
Importing the Daily API library
To import Daily's library, navigate to our app's Native Libraries and add the following line of code:
<script crossorigin src="https://unpkg.com/@daily-co/daily-js"></script>
Starting a call
Anvil lets you import and use JavaScript functions in Python code - handling all the conversion for you. Let's write the code our user interface needs to start a call.
Navigate back to our Form1
and, at the top of our Form's Code View, import the DailyIframe
class.
from anvil.js.window import DailyIframe
Then, back in the Form's Design View, create a click event handler for our start_call_button
.
The start_call_button_click
function will be called every time the button is clicked. In the function, we'll check if the user has entered a room name and then create an instance of a DailyIframe
called call_frame
. Then, we'll call the DailyIframe
's join()
method, passing it our meeting room link plus the room_name
as a parameter.
(Replace 'https://your-team.daily.co/'
with your own meeting room link, which you can find in your Daily dashboard.)
def start_call_button_click(self, **event_args): """This method is called when the button is clicked""" room_name = self.room_name_textbox.text if room_name: self.call_frame = DailyIframe.createFrame() self.call_frame.join({ 'url': 'https://anvil.daily.co/' + room_name })
What if that room doesn't exist?
If the specified room doesn't exist, the join()
method will throw an exception. We can catch this the usual way, with a try
block. Then we'll use Anvil's alert()
function to pop up a dialog:.
def start_call_button_click(self, **event_args): """This method is called when the button is clicked""" room_name = self.room_name_textbox.text if room_name: self.call_frame = DailyIframe.createFrame() try: self.call_frame.join({ 'url': 'https://anvil.daily.co/' + room_name }) except Exception as e: if "The meeting you're trying to join does not exist" in str(e): alert("That room doesn't exist.") self.end_call_button_click() else: raise
That's our start call functionality finished. Let's write the functionality that will end the call.
Ending a call
When the user clicks "End Call", we want to end the call, by calling the DailyIframe
's leave()
and destroy()
methods.
Like we did with start_call_button
, create an event handler function for end_call_button
called end_call_button_click
. Then call those methods:
def end_call_button_click(self, **event_args): """This method is called when the button is clicked""" self.call_frame.leave() self.call_frame.destroy()
Great work! Our app's users can now enter the name of the meeting they want to join, start and end the call all without ever leaving your Anvil app.
That's it!
We've just created a web app with nothing but Python; integrated it with Daily, and had it start a video call with the click of a button. Pretty cool, huh?
Daily call being startedClone the App
For those of you who want to see the finished source code for this app:
New to Anvil?
If you're new to Anvil, welcome! Anvil is a platform for building full-stack web apps with nothing but Python. No need to wrestle with JS, HTML, CSS, Python, SQL and all their frameworks – just build it all in Python.
Yes – Python that runs in the browser. Python that runs on the server. Python that builds your UI. A drag-and-drop UI editor. Anvil even has a built-in Python database, in case you don’t have your own.
Why not have a play with the app builder? It's free! Click here to get started:
Want to try a 5 minute tutorial? Try the Feedback Form tutorial.
Recommend
-
9
Announcing Anvil - A deployment dashboard for Laravel Forge ...
-
8
Anvil 0.4.0 + support librariesPreviously on Anvil: version 0.1 was the first one to use incremental rendering approach instead of virtual tree diff’ing. Version 0.2 added support for XML...
-
8
How to architect Anvil appsIf you only have a few UI elements, a small data class and your controllers have only a few actions to perform - you may consider using no architecture at all.Just make a global “controller” object a...
-
7
Anvil howto: view stylingA common question I hear about Anvil is how to style views.Let’s recall how it’s been traditionally done in Android. Here’s an example from the Android Dev...
-
3
Two way data bindings in AnvilNew Anvil 0.3.0 has been released. The major focus for this release has been on user input widgets and two-way data bindings.IssuesThere have been two types of issues in this two-way data...
-
5
Anvil: time for a changeThose of you who regularly check my blog may remember that I was going to redesign Anvil from scratch. Unfortunately, it was an unavoidable step....
-
10
Anvil: move fast and break thingsDear users, those who were early followers of Anvil. I’m now heavily working on the next version of Anvil, but it’s likely to make you rewrite your code, and I’m deeply sorry about that.Below I...
-
4
-
12
Add real- time, high- quality video chat into any app with our easy- to- bed interactive video SDK for web, mobile and native apps. launch structure moment! video conference SDK offers flexible customization, ease of perpetration, and dependable...
-
5
Creating Anvil-like annotation for Hilt using KSP Feb 3, 2024 5-minute read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK