In this example we are going to make another simple Dash App consisting of a title, a subtitle and an interactive bar graph.
If everything goes well, your App should look like this:
Run the following code in MATLAB®:
terminate(pyenv);
clearvars; % Removes all variables from the currently active workspace.
% Create Dash app
app = createApp();
uiFigure = uifigure('visible', 'off');
size = [3, 3];
uiGrid = uigridlayout(uiFigure, size);
% Title
title = uilabel(uiGrid, 'Text', 'Hello Dash',...
'FontSize', 50, 'FontWeight', 'bold');
subtitle = uilabel(uiGrid,...
'Text', 'Dash: A web application framework for your data.',...
'FontSize', 16);
subtitle.Layout.Row = 2;
fruit = categorical({'Apples', 'Oranges', 'Bananas'});
amount = [4 2; 1 4; 2 5];
city = {'SF'; 'Montreal'};
% Figure
ax = axes(uiGrid);
ax.Layout.Row = 3;
barChart = bar(ax, fruit, amount);
set(barChart, {'DisplayName'}, city);
xlabel(ax,'Fruit');
ylabel(ax,'Amount');
ax.Tag = 'example-graph'; % id for the bar graph
% Run the app.
layoutApp = startDash(uiGrid, 8057);
After running the code, you will see this message in the Command Window:
Dash is running on http://127.0.0.1:8057/
Serving Flask app ‘main’ (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: on
Open a new web browser and visit http://127.0.0.1:8057/. You should be able to see the app running!
The title and subtitle in this example are made using uilabel. uilabel can have stylings like FontSize and FontWeight.
For the bar graph we first define our axes in the uigridlayout (uiGrid), and then define our bar in those axes.
At the end we use the startDash function passing uiGrid as an argument, which contains all the elements of the app.
Dash Apps are interactive. For this example, if you place your cursor over one of the bars, the corresponding information appears.
The legend placed to the right of the plot allows you to filter data. Click on Montreal to remove that data. You will end up with a plot with only the SF data.
As you can see, making a Dash App is as easy as defining each element in a uigridlayout and running the app with this layout as an argument.
You can try to change the color of one of the texts (using FontColor) and include more information (maybe add a new city?) in the bar graph to see the changes. Have fun!
1 post - 1 participant


